Skip to content

use cases

Mahmoud Ben Hassine edited this page Nov 8, 2020 · 11 revisions

Use cases

There are many use cases where Easy Random can be useful to generate random data.

1. Generating test fixtures

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:

@org.junit.Test
public void testSortAlgo() {
   int[] ints = new EasyRandom().nextObject(int[].class);

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

   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:

@org.junit.Test
public void testPersistPerson() throws Exception {
   // given
   Person person = new EasyRandom().nextObject(Person.class);

   // when
   personDao.persist(person);

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

2. Populating a test database

Populating a database with random data is a common requirement. If you have already the code to persist your domain object into a database, let's say using a DAO or using JPA, then you can use Easy Random to generate random domain objects and then persist them. Here is a pseudo Java program that can be used:

EntityManager entityManager = entityManagerFactory.createEntityManager();
EasyRandom easyRandom = new EasyRandom();
for (int i = 0; i < 1000; i++) {
   entityManager.getTransaction().begin();
   entityManager.persist(easyRandom.nextObject(Person.class));
   // TODO commit only after x records
   entityManager.getTransaction().commit();
}
entityManager.close();

In this example, a EntityManager is used to persist 1000 randomly generated instances of the Person domain object.

3. Generating random data in a text file

This is another common requirement for testing batch applications. Once you have generated a random domain object with Easy Random, you can marshall it in a specific format (CSV, XML, JSON, etc) and write it in a text file that will serve as input to your batch application. Here is an example:

PrintWriter writer = new PrintWriter("persons.csv");
EasyRandom easyRandom = new EasyRandom();
for (int i = 0; i < 1000; i++) {
   Person person = easyRandom.nextObject(Person.class);
   writer.println(Utils.toCSV(person));
}
writer.close();

The Utils.toCSV() would write a person as a CSV record that is written to the output file. This could be also marshalling a Person object to XML or JSON using your favorite framework/tool.

4. Other use cases

There are plenty of other use cases where Easy Random can be handy:

  • Generating a random form bean to test the code of form validation
  • Generating a random model object in a MVC application to test the view rendering
  • Generating a high number of request objects to load-test a web service
  • Generating a random array of numbers to compare sorting algorithms
  • Generating a random response of a mocked REST service
  • etc..