-
Notifications
You must be signed in to change notification settings - Fork 233
excluding fields
Mahmoud Ben Hassine edited this page Nov 8, 2020
·
11 revisions
You can exclude fields from being populated in two ways:
class Person {
private String name;
@Exclude
private int age;
}
EasyRandomParameters parameters = new EasyRandomParameters().exclude(FieldPredicates.named("age"));
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
// or
EasyRandomParameters parameters = new EasyRandomParameters()
.exclude(FieldPredicates.ofType(Integer.class)); // this will exclude all fields of type Integer in the object graph
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
// or
EasyRandomParameters parameters = new EasyRandomParameters()
.randomize(FieldPredicates.named("age").and(FieldPredicates.ofType(Integer.class)).and(FieldPredicates.inClass(Person.class)), new SkipRandomizer());
EasyRandom easyRandom = new EasyRandom(parameters);
Person person = easyRandom.nextObject(Person.class);
The SkipRandomizer
is a typical implementation of the Null Object Pattern and allows you to skip fields from being populated.
Easy Random is created by Mahmoud Ben Hassine with the help of some awesome contributors!