Skip to content

excluding fields

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

Excluding fields

You can exclude fields from being populated in two ways:

Either declaratively with the @Exclude annotation:

class Person {

    private String name;

    @Exclude
    private int age;
}

Or programmatically like this:

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.