Class RecursiveAssertionConfiguration.Builder
- java.lang.Object
-
- org.assertj.core.api.recursive.AbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
-
- org.assertj.core.api.recursive.assertion.RecursiveAssertionConfiguration.Builder
-
- Enclosing class:
- RecursiveAssertionConfiguration
public static class RecursiveAssertionConfiguration.Builder extends AbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
Builder forRecursiveAssertionConfiguration
- Since:
- 3.24.0
-
-
Field Summary
Fields Modifier and Type Field Description private RecursiveAssertionConfiguration.CollectionAssertionPolicy
collectionAssertionPolicy
private boolean
ignoreAllNullFields
private boolean
ignorePrimitiveFields
private RecursiveAssertionIntrospectionStrategy
introspectionStrategy
private RecursiveAssertionConfiguration.MapAssertionPolicy
mapAssertionPolicy
private RecursiveAssertionConfiguration.OptionalAssertionPolicy
optionalAssertionPolicy
private boolean
skipJavaLibraryTypeObjects
-
Constructor Summary
Constructors Modifier Constructor Description private
Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RecursiveAssertionConfiguration
build()
RecursiveAssertionConfiguration.Builder
withCollectionAssertionPolicy(RecursiveAssertionConfiguration.CollectionAssertionPolicy policy)
Selects theRecursiveAssertionConfiguration.CollectionAssertionPolicy
to use for recursive application of aPredicate
to an object tree.RecursiveAssertionConfiguration.Builder
withIgnoreAllNullFields(boolean ignoreAllNullFields)
Makes the recursive assertion to ignore all null fields.RecursiveAssertionConfiguration.Builder
withIgnoredFields(java.lang.String... fieldsToIgnore)
Makes the recursive assertion to ignore the specified fields in the object under test.RecursiveAssertionConfiguration.Builder
withIgnoredFieldsMatchingRegexes(java.lang.String... regexes)
Makes the recursive assertion to ignore the fields matching the specified regexes in the object under test.RecursiveAssertionConfiguration.Builder
withIgnoredFieldsOfTypes(java.lang.Class<?>... types)
Makes the recursive assertion to ignore the object under test fields of the given types.RecursiveAssertionConfiguration.Builder
withIgnorePrimitiveFields(boolean ignorePrimitiveFields)
Choose between running thePredicate
in use over the primitive fields of an object in an object tree or not, by default asserting over primitives is enabled.RecursiveAssertionConfiguration.Builder
withIntrospectionStrategy(RecursiveAssertionIntrospectionStrategy introspectionStrategy)
Defines how objects are introspected in the recursive assertion.RecursiveAssertionConfiguration.Builder
withMapAssertionPolicy(RecursiveAssertionConfiguration.MapAssertionPolicy policy)
Selects theRecursiveAssertionConfiguration.MapAssertionPolicy
to use for recursive application of aPredicate
to an object tree.RecursiveAssertionConfiguration.Builder
withOptionalAssertionPolicy(RecursiveAssertionConfiguration.OptionalAssertionPolicy policy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.OptionalAssertionPolicy
.RecursiveAssertionConfiguration.Builder
withRecursionIntoJavaClassLibraryTypes(boolean recursionIntoJavaClassLibraryTypes)
Choose whether or not, while recursively applying aPredicate
to an object tree, the recursion will dive into types defined in the Java Class Library.
-
-
-
Field Detail
-
ignorePrimitiveFields
private boolean ignorePrimitiveFields
-
skipJavaLibraryTypeObjects
private boolean skipJavaLibraryTypeObjects
-
collectionAssertionPolicy
private RecursiveAssertionConfiguration.CollectionAssertionPolicy collectionAssertionPolicy
-
mapAssertionPolicy
private RecursiveAssertionConfiguration.MapAssertionPolicy mapAssertionPolicy
-
optionalAssertionPolicy
private RecursiveAssertionConfiguration.OptionalAssertionPolicy optionalAssertionPolicy
-
ignoreAllNullFields
private boolean ignoreAllNullFields
-
introspectionStrategy
private RecursiveAssertionIntrospectionStrategy introspectionStrategy
-
-
Method Detail
-
withIgnoredFields
public RecursiveAssertionConfiguration.Builder withIgnoredFields(java.lang.String... fieldsToIgnore)
Makes the recursive assertion to ignore the specified fields in the object under test.Example:
class Person { String name; String occupation; int age; Address address = new Address(); } class Address { int number; String street; } Person sherlock = new Person("Sherlock", "Detective", 60); sherlock.address.street = "Baker Street"; sherlock.address.number = 221; RecursiveAssertionConfiguration config = RecursiveAssertionConfiguration.builder() .withIgnoredFields("address", "age") .build(); // assertion succeeds Person has only String fields except for address and age assertThat(sherlock).usingRecursiveAssertion(config) .allFieldsSatisfy(field -> field instanceof String); // assertion fails because of age, address and address.number fields assertThat(sherlock).usingRecursiveComparison() .allFieldsSatisfy(field -> field instanceof String);
- Overrides:
withIgnoredFields
in classAbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
- Parameters:
fieldsToIgnore
- the fields to ignore in the object under test.- Returns:
- this builder.
-
withIgnoredFieldsMatchingRegexes
public RecursiveAssertionConfiguration.Builder withIgnoredFieldsMatchingRegexes(java.lang.String... regexes)
Makes the recursive assertion to ignore the fields matching the specified regexes in the object under test.Example:
class Person { String name; String occupation; int age; Address address = new Address(); } class Address { int number; String street; } Person sherlock = new Person("Sherlock", "Detective", 60); sherlock.address.street = "Baker Street"; sherlock.address.number = 221; RecursiveAssertionConfiguration config = RecursiveAssertionConfiguration.builder() .withIgnoredFieldsMatchingRegexes("ad.*", "ag.") .build(); // assertion succeeds Person has only String fields except for address and age assertThat(sherlock).usingRecursiveAssertion(config) .allFieldsSatisfy(field -> field instanceof String); // assertion fails because of age, address and address.number fields as by default no fields are ignored assertThat(sherlock).usingRecursiveComparison() .allFieldsSatisfy(field -> field instanceof String);
- Overrides:
withIgnoredFieldsMatchingRegexes
in classAbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
- Parameters:
regexes
- regexes used to ignore fields in the assertion.- Returns:
- this builder.
-
withIgnoreAllNullFields
public RecursiveAssertionConfiguration.Builder withIgnoreAllNullFields(boolean ignoreAllNullFields)
Makes the recursive assertion to ignore all null fields.Example:
class Person { String name; String occupation; Address address; } class Address { int number; String street; } Person sherlock = new Person("Sherlock", "Detective"); sherlock.address = null; RecursiveAssertionConfiguration config = RecursiveAssertionConfiguration.builder() .withIgnoreAllNullFields(true) .build(); // assertion succeeds as name and home.address.street fields are ignored in the comparison assertThat(noName).usingRecursiveAssertion(config) .allFieldsSatisfy(field -> field instanceof String); // assertion fails as name and home.address.street fields are populated for sherlock but not for noName. assertThat(sherlock).usingRecursiveComparison() .allFieldsSatisfy(field -> field instanceof String);
- Parameters:
ignoreAllNullFields
- whether to ignore empty optional fields in the recursive comparison- Returns:
- This builder.
-
withIgnorePrimitiveFields
public RecursiveAssertionConfiguration.Builder withIgnorePrimitiveFields(boolean ignorePrimitiveFields)
Choose between running thePredicate
in use over the primitive fields of an object in an object tree or not, by default asserting over primitives is enabled.For example, consider the following class:
class Example { public int primitiveField; public String objectField; }
By default, the assertion being applied recursively is applied to
primitiveField
and toobjectField
. If ignoring primitives it set to true, the assertion will only be applied toobjectField
.If you elect to assert over primitives then it is your own responsibility as a developer to ensure that your
Predicate
can handle (boxed) primitive arguments.- Parameters:
ignorePrimitiveFields
-true
to avoid asserting over primitives,false
to enable.- Returns:
- This builder.
-
withRecursionIntoJavaClassLibraryTypes
public RecursiveAssertionConfiguration.Builder withRecursionIntoJavaClassLibraryTypes(boolean recursionIntoJavaClassLibraryTypes)
Choose whether or not, while recursively applying a
Predicate
to an object tree, the recursion will dive into types defined in the Java Class Library. That is to say, whether or not to recurse into objects whose classes are declared in a package starting with java.* or javax.* .Consider the following example:
class Example { String s = "Don't look at me!"; } assertThat(new Example()).usingRecursiveAssertion(...).allFieldsSatisfy(o -> myPredicate(o));
With no recursion into Java Class Library types,
myPredicate()
is applied to the fields
but not to the internal fields ofString
. With recursion into Java standard types active, the internal fields of String will be examined as well.By default, recursion into Java Class Library types is disabled.
- Parameters:
recursionIntoJavaClassLibraryTypes
-true
to enable recursion into Java Class Library types,false
to disable it. Defaults tofalse
.- Returns:
- This builder.
-
withIgnoredFieldsOfTypes
public RecursiveAssertionConfiguration.Builder withIgnoredFieldsOfTypes(java.lang.Class<?>... types)
Makes the recursive assertion to ignore the object under test fields of the given types. The fields are ignored if their types exactly match one of the ignored types, for example if a field is a subtype of an ignored type it is not ignored.If some object under test fields are null it is not possible to evaluate their types and thus these fields are not ignored.
Example:
class Person { String name; String occupation; Address address = new Address(); } class Address { int number; String street; } Person sherlock = new Person("Sherlock", "Detective"); sherlock.address.street = "Baker Street"; sherlock.address.number = 221; RecursiveAssertionConfiguration config = RecursiveAssertionConfiguration.builder() .withIgnoredFieldsOfTypes(Address.class) .build(); // assertion succeeds Person has only String fields except for address assertThat(sherlock).usingRecursiveAssertion(config) .allFieldsSatisfy(field -> field instanceof String); // assertion fails because of address and address.number fields as the default config does not ignore any types. assertThat(sherlock).usingRecursiveComparison() .allFieldsSatisfy(field -> field instanceof String);
- Overrides:
withIgnoredFieldsOfTypes
in classAbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
- Parameters:
types
- the types we want to ignore in the object under test fields.- Returns:
- This builder.
-
withCollectionAssertionPolicy
public RecursiveAssertionConfiguration.Builder withCollectionAssertionPolicy(RecursiveAssertionConfiguration.CollectionAssertionPolicy policy)
Selects the
RecursiveAssertionConfiguration.CollectionAssertionPolicy
to use for recursive application of aPredicate
to an object tree. If not set, defaults toRecursiveAssertionConfiguration.CollectionAssertionPolicy.COLLECTION_OBJECT_AND_ELEMENTS
.Note that for the purposes of recursive asserting, an array counts as a collection, so this policy is applied to arrays as well as children of
Collection
.- Parameters:
policy
- The policy to use for collections and arrays in recursive asserting.- Returns:
- This builder.
-
withMapAssertionPolicy
public RecursiveAssertionConfiguration.Builder withMapAssertionPolicy(RecursiveAssertionConfiguration.MapAssertionPolicy policy)
Selects the
RecursiveAssertionConfiguration.MapAssertionPolicy
to use for recursive application of aPredicate
to an object tree. If not set, defaults toRecursiveAssertionConfiguration.MapAssertionPolicy.MAP_OBJECT_AND_ENTRIES
.- Parameters:
policy
- The policy to use for maps in recursive asserting.- Returns:
- This builder.
-
withOptionalAssertionPolicy
public RecursiveAssertionConfiguration.Builder withOptionalAssertionPolicy(RecursiveAssertionConfiguration.OptionalAssertionPolicy policy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.OptionalAssertionPolicy
.See
RecursiveAssertionConfiguration.OptionalAssertionPolicy
for the different possible policies, by defaultRecursiveAssertionConfiguration.OptionalAssertionPolicy.OPTIONAL_VALUE_ONLY
is used.- Parameters:
policy
- theRecursiveAssertionConfiguration.OptionalAssertionPolicy
to use.- Returns:
- This builder.
-
withIntrospectionStrategy
public RecursiveAssertionConfiguration.Builder withIntrospectionStrategy(RecursiveAssertionIntrospectionStrategy introspectionStrategy)
Defines how objects are introspected in the recursive assertion.Default to
DefaultRecursiveAssertionIntrospectionStrategy
that introspects all fields (including inherited ones).- Parameters:
introspectionStrategy
- theRecursiveAssertionIntrospectionStrategy
to use- Returns:
- This builder.
-
build
public RecursiveAssertionConfiguration build()
-
-