One of the main benefits of using EasyRandom is that it eliminates the need for writing boilerplate code to generate objects with random data. EasyRandom can generate objects with random values for all their fields with just a few lines of code. This means that developers can focus on testing or building their application logic rather than spending time creating random data.
Here is a question for you, which code snippet do you prefer:
In my opinion, the answer to this question is straightforward. Furthermore, the first code snippet could become significantly more complex if the classes utilized more fields or if we were using builders. On the other hand, the second code snippet should remain relatively unchanged unless unique edge cases arise.
Getting Started with EasyRandom
Before diving into the benefits and capabilities of EasyRandom, let’s first go through some basic examples of how to use it.
Setting up EasyRandom
First requires adding the EasyRandom dependency to your project. You can do this by adding the following code to your build.gradle file:
After adding the dependency, you can use EasyRandom in your tests.
Let’s say you have a simple User class:
To generate a random User object, you can use EasyRandom like this:
This will generate a random User object with random values for email, firstName, and lastName.
Let’s check it out in our test case:
Generating Collections and Maps
Collections as fields
Let’s say we have another user class called User2 with collections in fields. EasyRandom will generate these fields too.
Our test case:
Collections of simple data
We can create collections by using EasyRandom provided methods returning streams.
In this example, we can generate a List of Longs with provided size.
[-5106534569952410475, -167885730524958550, 4672433029010564658, -7216359497931550918, -3581075550420886390, -2298228485105199876, -5237980416576129062, 1326634973105178603, -3758321679654915806, -7771300887898959616]
Also, we can generate List with provided size and bounds.
[23, 28, 30, 30, 24, 25, 46, 28, 14, 23, 38, 17, 30, 45, 45, 29, 48, 48, 15, 45]
Collections of objects
EasyRandom has no problem with generating a list of objects. In similar manner:
Constraints on fields
Sometimes, we require objects or fields to be generated in a specific way, which may require us to make adjustments to our code generation. Let’s consider the following class as an example.
Our test will fail because we are still generating random fields.
We can easily fix it by adding our custom randomizer!
We can utilize it by providing it in EasyRandomParameters which then we will use with EasyRandom constructor.
In most cases, the need for custom randomizers will not arise as the EasyRandom library offers a vast collection of 81 built-in randomizer classes, each with numerous methods of creation readily available.
Currently (April 2023), the EasyRandom library does not support records. Merge requests are adding this functionality but waiting to be merged. However, a fork of the library is being developed by the community, and it supports generating records.
The Gradle file needs:
With record Car:
This test works:
Now that you’ve seen some practical examples of how EasyRandom functionality can make testing easier, you can now implement it into your code for your testing needs. This useful function of Kotlin can help you save some of your valuable time and other resources.
- Code above: https://gitlab.com/bettersolutions/in-house-projects/easy-random-article
- EasyRandom GitHub page: https://github.com/j-easy/easy-random
- EasyRandom fork with working records: https://github.com/dvgaba/easy-random
- Polish company made a wrapper on EasyRandom with their solutions. They also added record support. https://github.com/ocadotechnology/test-arranger
- Youtube video about Ocado solution (in Polish):