Skip to content
The simple, stupid random Java beans generator
Branch: master
Clone or download
Latest commit 816b0d6 Mar 26, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
easy-random-bean-validation Polish Mar 17, 2019
easy-random-core update javadoc Mar 17, 2019
easy-random-randomizers [maven-release-plugin] prepare for next development iteration Mar 14, 2019
.gitignore adding eclipse files to gitignore Dec 12, 2014
.travis.yml Travis: Add Java 12 build (openjdk-ea is now Java 13). Jan 13, 2019
LICENSE.txt update year in license headers Jan 26, 2019 Update Mar 26, 2019
pom.xml [maven-release-plugin] prepare for next development iteration Mar 14, 2019

Easy Random
The simple, stupid random Java™ beans generator

MIT license Coverage Status Build Status Maven Central Javadocs Gitter

Latest news

  • 14/03/2019: Random Beans has been renamed to Easy Random and is now part of Version 4.0.0.RC1 has been released! See what's new in the change log.
  • 13/03/2019: Version 3.9.0 is out! Checkout what's new in the change log.
  • 27/01/2019: Version 3.8.0 is finally out! Checkout what's new in the change log.

What is Easy Random ?

Easy Random is a library that generates random Java beans. You can think of it as an ObjectMother for the JVM. Let's say you have a class Person and you want to generate a random instance of it, here we go:

EasyRandom easyRandom = new EasyRandom();
Person person = easyRandom.nextObject(Person.class);

The method EasyRandom#nextObject is able to generate random instances of any given type.

What is this EasyRandom API?

The java.util.Random API provides 7 methods to generate random data: nextInt(), nextLong(), nextDouble(), nextFloat(), nextBytes(), nextBoolean() and nextGaussian(). What if you need to generate a random String? Or say a random instance of your domain object? Easy Random provides the EasyRandom API that extends java.util.Random with a method called nextObject(Class type). This method is able to generate a random instance of any arbitrary Java bean.

The EasyRandomParameters class is the main entry point to configure EasyRandom instances. It allows you to set all parameters to control how random data is generated:

EasyRandomParameters parameters = new EasyRandomParameters()
   .timeRange(nine, five)
   .dateRange(today, tomorrow)
   .stringLengthRange(5, 50)
   .collectionSizeRange(1, 10)

EasyRandom easyRandom = new EasyRandom(parameters);

For more details about these parameters, please refer to the configuration parameters section.

In most cases, default options are enough and you can use the default constructor of EasyRandom.

Easy Random allows you to control how to generate random data through the org.jeasy.random.api.Randomizer interface and makes it easy to exclude some fields from the object graph using a java.util.function.Predicate:

EasyRandomParameters parameters = new EasyRandomParameters()
   .randomize(String.class, () -> "foo")

EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);

In the previous example, Easy Random will:

  • Set all fields of type String to foo (using the Randomizer defined as a lambda expression)
  • Exclude the field named age of type Integer in class Person.

The static methods named, ofType and inClass are defined in org.jeasy.random.FieldPredicates which provides common predicates you can use in combination to define exactly which fields to exclude. A similar class called TypePredicates can be used to define which types to exclude from the object graph. You can of course use your own java.util.function.Predicate in combination with those predefined predicates.

Why Easy Random ?

Populating a Java object with random data can look easy at first glance, unless your domain model involves many related classes. In the previous example, let's suppose the Person type is defined as follows:

Without Easy Random, you would write the following code in order to create an instance of the Person class:

Street street = new Street(12, (byte) 1, "Oxford street");
Address address = new Address(street, "123456", "London", "United Kingdom");
Person person = new Person("Foo", "Bar", "", Gender.MALE, address);

And if these classes do not provide constructors with parameters (may be some legacy beans you can't change), you would write:

Street street = new Street();
street.setType((byte) 1);
street.setName("Oxford street");

Address address = new Address();
address.setCountry("United Kingdom");

Person person = new Person();

With Easy Random, generating a random Person object is done with new EasyRandom().nextObject(Person.class). The library will recursively populate all the object graph. That's a big difference!

How can this be useful ?

Sometimes, the test fixture does not really matter to the test logic. For example, if we want to test the result of a new sorting algorithm, we can generate random input data and assert the output is sorted, regardless of the data itself:

public void testSortAlgorithm() {

   // Given
   int[] ints = easyRandom.nextObject(int[].class);

   // When
   int[] sortedInts = myAwesomeSortAlgo.sort(ints);

   // Then
   assertThat(sortedInts).isSorted(); // fake assertion


Another example is testing the persistence of a domain object, we can generate a random domain object, persist it and assert the database contains the same values:

public void testPersistPerson() throws Exception {
   // Given
   Person person = easyRandom.nextObject(Person.class);

   // When

   // Then
   assertThat("person_table").column("name").value().isEqualTo(person.getName()); // assretj db

There are many other uses cases where Easy Random can be useful, you can find a non exhaustive list in the wiki.



You are welcome to contribute to the project with pull requests on GitHub.

If you believe you found a bug, please use the issue tracker.

If you have any question, suggestion, or feedback, do not hesitate to use the Gitter channel of the project.

Core team and contributors

Core team

Awesome contributors

Thank you all for your contributions!

You can’t perform that action at this time.