-
Notifications
You must be signed in to change notification settings - Fork 232
use cases
The are many use cases where Random Beans can be useful to generate random data.
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 simply use Random Beans to generate random domain objects and then persist them. Here is a pseudo Java program that can be used:
EntityManager entityManager = entityManagerFactory.createEntityManager();
Populator populator = new PopulatorBuilder().build();
for (int i = 0; i < 1000; i++) {
entityManager.getTransaction().begin();
entityManager.persist(populator.populateBean(Person.class));
entityManager.getTransaction().commit();
}
entityManager.close();
In this example, a EntityManager
is used to persist 1000 randomly generated instances of the Person
domain object.
This is another common requirement for testing batch applications. Once you have generated a random domain object with Random Beans, you can write it in a specific format (CSV, XML, JSON, etc) in a text file that will serve as input to your batch application. Here is an example:
PrintWriter writer = new PrintWriter("persons.csv");
Populator populator = new PopulatorBuilder().build();
for (int i = 0; i < 1000; i++) {
Person person = populator.populateBean(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.
There are plenty of other use cases where Random Beans 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
- etc..
Easy Random is created by Mahmoud Ben Hassine with the help of some awesome contributors!