Skip to content

excluding fields

Mahmoud Ben Hassine edited this page Feb 29, 2016 · 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:

EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder().build();
Person person = enhancedRandom.nextObject(Person.class, "age");

// or

EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
   .exclude(FieldDefinitionBuilder.field().named("age").ofType(Integer.class).inClass(Person.class).get())
   .build();
Person person = enhancedRandom.nextObject(Person.class);

// or

EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
   .randomize(FieldDefinitionBuilder.field().named("age").ofType(Integer.class).inClass(Person.class).get(), new SkipRandomizer())
   .build();
Person person = enhancedRandom.nextObject(Person.class);

The second argument of the enhancedRandom.nextObject method is a var arg list of fields to exclude.

The SkipRandomizer is a typical implementation of the Null Object Pattern and allows you to skip fields from being populated.

Clone this wiki locally