Top Description Fields Constructors Methods
org.assertj.core.api

public Class RecursiveAssertionAssert

extends AbstractAssert<RecursiveAssertionAssert, Object>
Class Inheritance
Annotations
@Beta
Static Imports
org.assertj.core.error.ShouldNotSatisfyPredicateRecursively.shouldNotSatisfyRecursively

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

Modifier and TypeField and Description
private final RecursiveAssertionConfiguration
private final RecursiveAssertionDriver
Inherited from org.assertj.core.api.AbstractAssert:
actualassertionErrorCreatorconditionscustomRepresentationinfomyselfobjectsprintAssertionsDescriptionthrowUnsupportedExceptionOnEquals

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public RecursiveAssertionAssert

Returns:

this assertions object
allFieldsSatisfy
(Predicate<Object>
The predicate that is recursively applied to all the fields in the object tree of which actual is the root.
predicate
)

Asserts that the given predicate is met for all fields of the object under test recursively (but not the object itself).

public RecursiveAssertionAssert

Returns:

this assertions object
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).

public RecursiveAssertionAssert

Returns:

this RecursiveAssertionAssert to chain other methods.
ignoringAllNullFields
()

Makes the recursive assertion to ignore all null fields.

public RecursiveAssertionAssert

Returns:

this RecursiveAssertionAssert to chain other methods.
ignoringFields
(String...
the fields to ignore in the object under test.
fieldsToIgnore
)

Makes the recursive assertion to ignore the specified fields in the object under test.

public RecursiveAssertionAssert

Returns:

this RecursiveAssertionAssert to chain other methods.
ignoringFieldsMatchingRegexes
(String...
regexes used to ignore fields in the assertion.
regexes
)

Makes the recursive assertion to ignore the fields matching the specified regexes in the object under test.

public RecursiveAssertionAssert

Returns:

this RecursiveAssertionAssert to chain other methods.
ignoringFieldsOfTypes
(Class<?>...
the types we want to ignore in the object under test fields.
typesToIgnore
)

Makes the recursive assertion to ignore the object under test fields of the given types.

public RecursiveAssertionAssert

Returns:

this RecursiveAssertionAssert to chain other methods.
ignoringPrimitiveFields
()

Make the recursive assertion not to run the Predicate over the primitive fields of an object in an object graph, by default asserting over primitives is enabled.

public RecursiveAssertionAssert
public RecursiveAssertionAssert

Returns:

this RecursiveAssertionAssert to chain other methods.
withIntrospectionStrategy
(RecursiveAssertionIntrospectionStrategy introspectionStrategy)

Defines how objects are introspected in the recursive assertion.

public RecursiveAssertionAssert
public RecursiveAssertionAssert
Inherited from org.assertj.core.api.AbstractAssert:
areEqualasInstanceOfasListassertionErrorasStringdescribedAsdescriptionTextdoesNotHavedoesNotHaveSameClassAsdoesNotHaveSameHashCodeAsdoesNotHaveToStringdoesNotHaveToStringequalsextractingextractingfailurefailureWithActualExpectedfailWithActualExpectedAndMessagefailWithMessagegetWritableAssertionInfohashashCodehasSameClassAshasSameHashCodeAshasToStringhasToStringinBinaryinHexadecimalisisElementOfCustomAssertisEqualToisExactlyInstanceOfisInisInisInstanceOfisInstanceOfAnyisInstanceOfSatisfyingisNotisNotEqualToisNotExactlyInstanceOfisNotInisNotInisNotInstanceOfisNotInstanceOfAnyisNotNullisNotOfAnyClassInisNotSameAsisNullisOfAnyClassInisSameAsmatchesmatchesnewListAssertInstanceoverridingErrorMessageoverridingErrorMessagesatisfiessatisfiessatisfiessatisfiesAnyOfsatisfiesAnyOfsatisfiesAnyOfForProxysatisfiesForProxysetCustomRepresentationsetDescriptionConsumersetPrintAssertionsDescriptionthrowAssertionErrorusingComparatorusingComparatorusingDefaultComparatorusingRecursiveAssertionusingRecursiveAssertionusingRecursiveComparisonusingRecursiveComparisonwithAssertionStatewithFailMessagewithFailMessagewithRepresentationwithThreadDumpOnError

Field Detail

recursiveAssertionConfigurationback to summary
private final RecursiveAssertionConfiguration recursiveAssertionConfiguration
recursiveAssertionDriverback to summary
private final RecursiveAssertionDriver recursiveAssertionDriver

Constructor Detail

RecursiveAssertionAssertback to summary
public RecursiveAssertionAssert(Object o, RecursiveAssertionConfiguration recursiveAssertionConfiguration)

Method Detail

allFieldsSatisfyback to summary
public RecursiveAssertionAssert allFieldsSatisfy(Predicate<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 java.util.function.Predicate applied to them (including primitive fields), no fields are excluded, but:

You can change how the recursive assertion deals with arrays, collections, maps and optionals, see:

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", "[email protected]");
Author martinFowler = new Author("Martin Fowler", "[email protected]");
Author kentBeck = new Author("Kent Beck", "[email protected]");

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:Predicate<Object>

The predicate that is recursively applied to all the fields in the object tree of which actual is the root.

Returns:RecursiveAssertionAssert

this assertions object

Exceptions
AssertionError:
if one or more fields as described above fail the predicate test.
hasNoNullFieldsback to summary
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", "[email protected]");
Author martinFowler = new Author("Martin Fowler", "[email protected]");
Author kentBeck = new Author("Kent Beck", "[email protected]");

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:RecursiveAssertionAssert

this assertions object

Exceptions
AssertionError:
if one or more fields as described above are null.
ignoringAllNullFieldsback to summary
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:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

ignoringFieldsback to summary
public RecursiveAssertionAssert ignoringFields(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:String[]

the fields to ignore in the object under test.

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

ignoringFieldsMatchingRegexesback to summary
public RecursiveAssertionAssert ignoringFieldsMatchingRegexes(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:String[]

regexes used to ignore fields in the assertion.

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

ignoringFieldsOfTypesback to summary
public RecursiveAssertionAssert ignoringFieldsOfTypes(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:Class<?>[]

the types we want to ignore in the object under test fields.

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

ignoringPrimitiveFieldsback to summary
public RecursiveAssertionAssert ignoringPrimitiveFields()

Make the recursive assertion not to run the Predicate 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 to objectField. If ignoring primitives it set to true, the assertion will only be applied to objectField.

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:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

withCollectionAssertionPolicyback to summary
public RecursiveAssertionAssert withCollectionAssertionPolicy(RecursiveAssertionConfiguration.CollectionAssertionPolicy collectionAssertionPolicy)

Makes the recursive assertion to use the specified RecursiveAssertionConfiguration.CollectionAssertionPolicy.

See RecursiveAssertionConfiguration.CollectionAssertionPolicy for the different possible policies, by default RecursiveAssertionConfiguration.CollectionAssertionPolicy#ELEMENTS_ONLY is used.

Parameters
collectionAssertionPolicy:RecursiveAssertionConfiguration.CollectionAssertionPolicy

the RecursiveAssertionConfiguration.CollectionAssertionPolicy to use.

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

withIntrospectionStrategyback to summary
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:RecursiveAssertionIntrospectionStrategy

the RecursiveAssertionIntrospectionStrategy to use

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

withMapAssertionPolicyback to summary
public RecursiveAssertionAssert withMapAssertionPolicy(RecursiveAssertionConfiguration.MapAssertionPolicy mapAssertionPolicy)

Makes the recursive assertion to use the specified RecursiveAssertionConfiguration.MapAssertionPolicy.

See RecursiveAssertionConfiguration.MapAssertionPolicy for the different possible policies, by default RecursiveAssertionConfiguration.MapAssertionPolicy#MAP_VALUES_ONLY is used.

Parameters
mapAssertionPolicy:RecursiveAssertionConfiguration.MapAssertionPolicy

the RecursiveAssertionConfiguration.MapAssertionPolicy to use.

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.

withOptionalAssertionPolicyback to summary
public RecursiveAssertionAssert withOptionalAssertionPolicy(RecursiveAssertionConfiguration.OptionalAssertionPolicy optionalAssertionPolicy)

Makes the recursive assertion to use the specified RecursiveAssertionConfiguration.OptionalAssertionPolicy.

See RecursiveAssertionConfiguration.OptionalAssertionPolicy for the different possible policies, by default RecursiveAssertionConfiguration.OptionalAssertionPolicy#OPTIONAL_VALUE_ONLY is used.

Parameters
optionalAssertionPolicy:RecursiveAssertionConfiguration.OptionalAssertionPolicy

the RecursiveAssertionConfiguration.OptionalAssertionPolicy to use.

Returns:RecursiveAssertionAssert

this RecursiveAssertionAssert to chain other methods.