Dummie is a simple data generator for preparing test data, it provides an easy way to construct complex data class as below:
TargetClass instance = Dummie.create(TargetClass.class);
- Add Dependency
compile "io.github.dummie-java:dummie:${latestVersion}"
When you writing test code, you may want to create multi complicted data like:
public User People {
private Long id;
private String name;
private Integer age;
...
//getter & setter
}
public class Employee extend User {
private Long id;
private String mobile;
private String email;
private List<Asset> assets;
...
//getter & setter
}
public class Asset {
private Long id;
private String name;
private String desc;
...
//getter & setter
}
The only thing you want do is create an Employee instance with fullfilled datas and deliver to other method. With Dummie, you can use simple one line code to get what your want:
Employee dummieEmployee = Dummie.create(Employee.class);
Maybe you want some fields special in the dummy Employee.
- Suppose in next test, we want all asset name has same value, so we can use
override
method:
Employee dummieEmployee = Dummie.prepare().override("desc", "value").create(Employee.class);
so all fields named desc and with string type should be filled to value "value", dummie determine type based on override
second parameter type.
- If you want all fields with the type
Long
to be set 100L, you can:
Employee dummieEmployee = Dummie.prepare().override(Long.class, 100L).create(Employee.class);
- We can also use regex expression to override field values like:
Employee dummieEmployee = Dummie.prepare().override(".*Time", new ZonedDateTime()).create(Employee.class);
- Suppose we want all id would be random value, we can use
random
method:
Employee dummieEmployee = Dummie.prepare().random("id").create(Employee.class);
so all fields named id would be filled by random value.
- Like
override
, random can also special a kind of type:
Employee dummieEmployee = Dummie.prepare().random(String.class).create(Employee.class);
Employee dummieEmployee = Dummie.prepare()
.valueGenerator(someGenerator)
.create(Employee.class);
Dummie uses static value for data generate by default. If you want all field values use random value generator, you should set GenerationStrategy:
Employee dummieEmployee = Dummie.withStrategy(GenerationStrategy.RANDOM).create(Employee.class);
Configuration configuration = new Configuration(cycleLogic, strategy, dataCache, dataGenerator);
Employee dummieEmployee = Dummie.withConfiguration(configuration).create(Employee.class);
If the target class fields have cycle reference, Dummie has two logic to deal with.
By default, Dummie cache field value, after that when Dummie find the same field, it will return the cache value.
And Dummie also support generate by deep, if you want use you can set
Employee dummieEmployee = Dummie.cycleLogic(CycleLogic.LEVEL)
.withFloor(limit) // Optional, set cycle deep level, default value is 2.
.create(Employee.class);