Mahmoud Ben Hassine edited this page Sep 8, 2017 · 18 revisions

What is Random Beans ?

Random Beans is a library that generates random Java beans. Let's say you have a class Person and you want to generate a random instance of it, here we go:

Person person = random(Person.class);

The static method EnhancedRandom.random is able to generate random instances of a given type.

Let's see another example. If you want to generate a random stream of 10 persons, you can use the following snippet:

Stream<Person> persons = randomStreamOf(10, Person.class);

This second static method of the EnhancedRandom API generates a stream of random instances of a given type.

What is this EnhancedRandom 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? Random Beans provides the EnhancedRandom API that extends (enhances) java.util.Random with a method called nextObject(Class type). This method is able to generate a random instance of any arbitrary Java bean:

EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder().build();
Person person = enhancedRandom.nextObject(Person.class);

The EnhancedRandomBuilder is the main entry point to configure EnhancedRandom instances with a fluent API. It allows you to set all parameters to control how random data is generated:

EnhancedRandom random = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
   .timeRange(nine, five)
   .dateRange(today, tomorrow)
   .stringLengthRange(5, 50)
   .collectionSizeRange(1, 10)

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

In most cases, default options are enough and you can use a static import of the EnhancedRandom.random(Class object) as seen previously.

Why Random Beans ?

Populating a Java object with random data may 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 Random Beans, 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 don't have the control over), 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 Random Beans, generating a random Person object is done with random(Person.class). The library will recursively populate all the object graph. That's a big difference!

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.