Class RecursiveAssertionAssert
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<RecursiveAssertionAssert,java.lang.Object>
-
- org.assertj.core.api.RecursiveAssertionAssert
-
- All Implemented Interfaces:
Assert<RecursiveAssertionAssert,java.lang.Object>
,Descriptable<RecursiveAssertionAssert>
,ExtensionPoints<RecursiveAssertionAssert,java.lang.Object>
public class RecursiveAssertionAssert extends AbstractAssert<RecursiveAssertionAssert,java.lang.Object>
An assertion that supports asserting a
Predicate
over all the fields of an object graph. Cycle avoidance is used, so a graph that has cyclic references is essentially reduced to a tree by this class (the actual object graph is not changed of course, it is treated as an immutable value).- Since:
- 3.24.0
-
-
Field Summary
Fields Modifier and Type Field Description private RecursiveAssertionConfiguration
recursiveAssertionConfiguration
private RecursiveAssertionDriver
recursiveAssertionDriver
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, customRepresentation, info, myself, objects, printAssertionsDescription, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Constructor Description RecursiveAssertionAssert(java.lang.Object o, RecursiveAssertionConfiguration recursiveAssertionConfiguration)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RecursiveAssertionAssert
allFieldsSatisfy(java.util.function.Predicate<java.lang.Object> predicate)
Asserts that the given predicate is met for all fields of the object under test recursively (but not the object itself).RecursiveAssertionAssert
hasNoNullFields()
Asserts that none of the fields of the object under test graph (i.e.RecursiveAssertionAssert
ignoringAllNullFields()
Makes the recursive assertion to ignore all null fields.RecursiveAssertionAssert
ignoringFields(java.lang.String... fieldsToIgnore)
Makes the recursive assertion to ignore the specified fields in the object under test.RecursiveAssertionAssert
ignoringFieldsMatchingRegexes(java.lang.String... regexes)
Makes the recursive assertion to ignore the fields matching the specified regexes in the object under test.RecursiveAssertionAssert
ignoringFieldsOfTypes(java.lang.Class<?>... typesToIgnore)
Makes the recursive assertion to ignore the object under test fields of the given types.RecursiveAssertionAssert
ignoringPrimitiveFields()
Make the recursive assertion not to run thePredicate
over the primitive fields of an object in an object graph, by default asserting over primitives is enabled.RecursiveAssertionAssert
withCollectionAssertionPolicy(RecursiveAssertionConfiguration.CollectionAssertionPolicy collectionAssertionPolicy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.CollectionAssertionPolicy
.RecursiveAssertionAssert
withIntrospectionStrategy(RecursiveAssertionIntrospectionStrategy introspectionStrategy)
Defines how objects are introspected in the recursive assertion.RecursiveAssertionAssert
withMapAssertionPolicy(RecursiveAssertionConfiguration.MapAssertionPolicy mapAssertionPolicy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.MapAssertionPolicy
.RecursiveAssertionAssert
withOptionalAssertionPolicy(RecursiveAssertionConfiguration.OptionalAssertionPolicy optionalAssertionPolicy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.OptionalAssertionPolicy
.-
Methods inherited from class org.assertj.core.api.AbstractAssert
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison, withAssertionState, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.Descriptable
as, as, as, describedAs, describedAs
-
-
-
-
Field Detail
-
recursiveAssertionConfiguration
private final RecursiveAssertionConfiguration recursiveAssertionConfiguration
-
recursiveAssertionDriver
private final RecursiveAssertionDriver recursiveAssertionDriver
-
-
Constructor Detail
-
RecursiveAssertionAssert
public RecursiveAssertionAssert(java.lang.Object o, RecursiveAssertionConfiguration recursiveAssertionConfiguration)
-
-
Method Detail
-
allFieldsSatisfy
public RecursiveAssertionAssert allFieldsSatisfy(java.util.function.Predicate<java.lang.Object> predicate)
Asserts that the given predicate is met for all fields of the object under test recursively (but not the object itself).
For example if the object under test is an instance of class A, A has a B field and B a C field then the assertion checks A's B field and B's C field and all C's fields.
The recursive algorithm employs cycle detection, so object graphs with cyclic references can safely be asserted over without causing looping.
This method enables recursive asserting using default configuration, which means all fields of all objects have the
Predicate
applied to them (including primitive fields), no fields are excluded, but:- The recursion does not enter into Java Class Library types (java.*, javax.*)
- The
Predicate
is applied toCollection
and array elements (but not the collection/array itself) - The
Predicate
is applied toMap
values but not the map itself or its keys - The
Predicate
is applied toOptional
and primitive optional values
You can change how the recursive assertion deals with arrays, collections, maps and optionals, see:
withCollectionAssertionPolicy(RecursiveAssertionConfiguration.CollectionAssertionPolicy)
for collections and arrayswithMapAssertionPolicy(RecursiveAssertionConfiguration.MapAssertionPolicy)
for mapswithOptionalAssertionPolicy(RecursiveAssertionConfiguration.OptionalAssertionPolicy)
for optionals
It is possible to assert several predicates over the object graph in a row.
The classes used in recursive asserting are not thread safe. Care must be taken when running tests in parallel not to run assertions over object graphs that are being shared between tests.
Example:
class Author { String name; String email; List<Book> books = new ArrayList<>(); Author(String name, String email) { this.name = name; this.email = email; } } class Book { String title; Author[] authors; Book(String title, Author[] authors) { this.title = title; this.authors = authors; } } Author pramodSadalage = new Author("Pramod Sadalage", "p.sadalage@recursive.test"); Author martinFowler = new Author("Martin Fowler", "m.fowler@recursive.test"); Author kentBeck = new Author("Kent Beck", "k.beck@recursive.test"); Book noSqlDistilled = new Book("NoSql Distilled", new Author[] {pramodSadalage, martinFowler}); pramodSadalage.books.add(noSqlDistilled); martinFowler.books.add(noSqlDistilled); Book refactoring = new Book("Refactoring", new Author[] {martinFowler, kentBeck}); martinFowler.books.add(refactoring); kentBeck.books.add(refactoring); // assertion succeeds assertThat(pramodSadalage).usingRecursiveAssertion() .allFieldsSatisfy(field -> field != null);
- Parameters:
predicate
- The predicate that is recursively applied to all the fields in the object tree of which actual is the root.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if one or more fields as described above fail the predicate test.
-
hasNoNullFields
public RecursiveAssertionAssert hasNoNullFields()
Asserts that none of the fields of the object under test graph (i.e. recursively getting the fields) are null (but not the object itself).This is a convenience method for a common test, and it is equivalent to
allFieldsSatisfy(field -> field != null)
.Example:
class Author { String name; String email; List<Book> books = new ArrayList<>(); Author(String name, String email) { this.name = name; this.email = email; } } class Book { String title; Author[] authors; Book(String title, Author[] authors) { this.title = title; this.authors = authors; } } Author pramodSadalage = new Author("Pramod Sadalage", "p.sadalage@recursive.test"); Author martinFowler = new Author("Martin Fowler", "m.fowler@recursive.test"); Author kentBeck = new Author("Kent Beck", "k.beck@recursive.test"); Book noSqlDistilled = new Book("NoSql Distilled", new Author[]{pramodSadalage, martinFowler}); pramodSadalage.books.add(noSqlDistilled); martinFowler.books.add(noSqlDistilled); Book refactoring = new Book("Refactoring", new Author[] {martinFowler, kentBeck}); martinFowler.books.add(refactoring); kentBeck.books.add(refactoring); // assertion succeeds assertThat(pramodSadalage).usingRecursiveAssertion() .hasNoNullFields();
- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if one or more fields as described above are null.
-
ignoringFields
public RecursiveAssertionAssert ignoringFields(java.lang.String... fieldsToIgnore)
Makes the recursive assertion to ignore the specified fields in the object under test.When a field is ignored, all its fields are ignored too.
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; // assertion succeeds because Person has only String fields except for address and age (address fields are ignored) assertThat(sherlock).usingRecursiveAssertion() .ignoringFields("address", "age") .allFieldsSatisfy(field -> field instanceof String); // assertion fails because of age, address and address.number fields assertThat(sherlock).usingRecursiveAssertion() .allFieldsSatisfy(field -> field instanceof String);
- Parameters:
fieldsToIgnore
- the fields to ignore in the object under test.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
ignoringFieldsMatchingRegexes
public RecursiveAssertionAssert ignoringFieldsMatchingRegexes(java.lang.String... regexes)
Makes the recursive assertion to ignore the fields matching the specified regexes in the object under test.When a field is ignored, all its fields are ignored too.
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; // assertion succeeds because Person has only String fields except for address and age (address fields are ignored) assertThat(sherlock).usingRecursiveAssertion() .ignoringFieldsMatchingRegexes("ad.*", "ag.") .allFieldsSatisfy(field -> field instanceof String); // assertion fails because of age and address fields (address.number is ignored) assertThat(sherlock).usingRecursiveAssertion() .ignoringFieldsMatchingRegexes(".*ber") .allFieldsSatisfy(field -> field instanceof String);
- Parameters:
regexes
- regexes used to ignore fields in the assertion.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
ignoringFieldsOfTypes
public RecursiveAssertionAssert ignoringFieldsOfTypes(java.lang.Class<?>... typesToIgnore)
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.
When a field is ignored, all its fields are ignored too.
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; // assertion succeeds because Person has only String fields except for address (address fields are ignored) assertThat(sherlock).usingRecursiveAssertion() .ignoringFieldsOfTypes(Address.class) .allFieldsSatisfy(field -> field instanceof String); // assertion fails because of address and address.number fields assertThat(sherlock).usingRecursiveAssertion() .allFieldsSatisfy(field -> field instanceof String);
- Parameters:
typesToIgnore
- the types we want to ignore in the object under test fields.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
ignoringPrimitiveFields
public RecursiveAssertionAssert ignoringPrimitiveFields()
Make the recursive assertion not to run thePredicate
over the primitive fields of an object in an object graph, 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 will be 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.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
ignoringAllNullFields
public RecursiveAssertionAssert ignoringAllNullFields()
Makes the recursive assertion to ignore all null fields.class Person { String name; String occupation; Address address; } class Address { int number; String street; } Person sherlock = new Person("Sherlock", "Detective"); sherlock.address = null; // assertion succeeds as address field is ignored assertThat(noName).usingRecursiveAssertion() .ignoringAllNullFields() .allFieldsSatisfy(field -> field instanceof String); // assertion fails as address, address.number and address.street fields are not evaluated as String, street because it's null. assertThat(sherlock).usingRecursiveAssertion() .allFieldsSatisfy(field -> field instanceof String);
- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
withOptionalAssertionPolicy
public RecursiveAssertionAssert withOptionalAssertionPolicy(RecursiveAssertionConfiguration.OptionalAssertionPolicy optionalAssertionPolicy)
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:
optionalAssertionPolicy
- theRecursiveAssertionConfiguration.OptionalAssertionPolicy
to use.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
withCollectionAssertionPolicy
public RecursiveAssertionAssert withCollectionAssertionPolicy(RecursiveAssertionConfiguration.CollectionAssertionPolicy collectionAssertionPolicy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.CollectionAssertionPolicy
.See
RecursiveAssertionConfiguration.CollectionAssertionPolicy
for the different possible policies, by defaultRecursiveAssertionConfiguration.CollectionAssertionPolicy.ELEMENTS_ONLY
is used.- Parameters:
collectionAssertionPolicy
- theRecursiveAssertionConfiguration.CollectionAssertionPolicy
to use.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
withMapAssertionPolicy
public RecursiveAssertionAssert withMapAssertionPolicy(RecursiveAssertionConfiguration.MapAssertionPolicy mapAssertionPolicy)
Makes the recursive assertion to use the specifiedRecursiveAssertionConfiguration.MapAssertionPolicy
.See
RecursiveAssertionConfiguration.MapAssertionPolicy
for the different possible policies, by defaultRecursiveAssertionConfiguration.MapAssertionPolicy.MAP_VALUES_ONLY
is used.- Parameters:
mapAssertionPolicy
- theRecursiveAssertionConfiguration.MapAssertionPolicy
to use.- Returns:
- this
RecursiveAssertionAssert
to chain other methods.
-
withIntrospectionStrategy
public RecursiveAssertionAssert 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
RecursiveAssertionAssert
to chain other methods.
-
-