-
Notifications
You must be signed in to change notification settings - Fork 1
Generators
Generators are the heart of Factorium. A generator is simply a class that describes how to create a valid object of any desired type. To make your own generator, it is recommanded to extend the provided BaseGenerator<T> class. It provides a basic implementation that can handle most scenarios of modifiers and leaves you to focus on creating your object. Furthermore, it future-proofs your application against any possible changes as it is guaranteed that any class that extends it will only ever have to implement the make() method.
FakerGenerator is a base generator that additionnally provides a shared Faker instance for all your subclasses. You may configure it once and have it available with that configuration everywhere.
Here is an example of a simple POJO and it's associated generator:
public class City {
private String name;
private long nCitizens;
public City(String name, long nCitizens) {
this.name = name;
this.nCitizens = nCitizens;
}
...
}
public class CityGenerator extends FakerGenerator<City> {
@Override
protected City make() {
return new City(faker.name().lastName(), Math.abs(faker.random().nextLong()) + 1);
}
}After that initial bit of configuration, creating a city for your tests becomes as simple as:
CityGenerator generator = new CityGenerator();
City foo = generator.generate();You may have as many generators as you want for a given class. It is suggested to keep all of your generators for a type in the same file by using static nested classes, with the top level one being the default one.