Europe Union
Back Back to Blog
Published: 04/09/2023

Simplify Your Testing with EasyRandom

EasyRandom is a Java library that allows developers to easily generate random objects, which can be useful for testing, prototyping, and data seeding. It is a powerful tool that can save time and effort when working with object-oriented programming languages like Java.

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.

Basic Usage

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:

Printed user:

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:

Printed User2:

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.

Printed values:

[-5106534569952410475, -167885730524958550, 4672433029010564658, -7216359497931550918, -3581075550420886390, -2298228485105199876, -5237980416576129062, 1326634973105178603, -3758321679654915806, -7771300887898959616]

Also, we can generate List with provided size and bounds.

Printed values:

[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:

Special cases

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.

Printed text:

User3{email='eOMtThyhVNLWUZNRcBaQKxI', firstName='yedUsFwdkelQbxeTeQOvaScfqIOOmaa'}

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.

Printed text:

User3{email='r3WATL5Ifh@net', firstName='pgly'}


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.

Useful links

Honorable mentions:

Estimate your project.

Just leave your email address and we’ll be in touch soon
ornament ornament