Skip to content
Mahmoud Ben Hassine edited this page Mar 20, 2015 · 19 revisions

Introduction

Testing applications against random input, also known as Monkey Tests 🐵, is an essential step to improve stability and robustness.

There are many use cases where one may need to generate random data during tests such as:

  • Populating a test database with random data
  • Generating random input file for a batch application
  • Generating random model to test view rendering in a MVC web application
  • Generating random form input to test form validation
  • etc

Generating random data is a tedious task, especially when the domain model involves many related classes. This is where jPopulator comes to play, to help you populating your domain objects easily with random data.

So if you love Monkey tests, jPopulator will be your best friend!

What is jPopulator?

jPopulator is a java library that allows you to populate java beans with random data.

Populating a deep type hierarchy by hand is a tedious task and the goal of jPopulator is to make this task hassle-free. Let's see an example, suppose you have the following classes:

If you want to populate a Person bean with jPopulator, you simply write this:

Populator populator = new PopulatorBuilder().build();
Person person = populator.populateBean(Person.class);

And voila! jPopulator will introspect the Person type hierarchy, generate an instance for each nested bean and populate it with random data.

Without jPopulator, you would write the following code:

Street street = new Street(12, (byte) 1, "Oxford street");
Address address = new Address(street, "123456", "London", "United Kingdom");
Person person = new Person("Foo", "Bar", "[email protected]", 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.setNumber(12);
street.setType((byte) 1);
street.setName("Oxford street");

Address address = new Address();
address.setStreet(street);
address.setZipCode("123456");
address.setCity("London");
address.setCountry("United Kingdom");

Person person = new Person();
person.setFirstName("Foo");
person.setLastName("Bar");
person.setEmail("[email protected]");
person.setGender(Gender.MALE);
person.setAddress(address);

As you can see, jPopulator can save you from writing all this bean populating code.