Class RecursiveAssertionConfiguration.Builder

Enclosing class:
RecursiveAssertionConfiguration

public static class RecursiveAssertionConfiguration.Builder extends AbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
Since:
3.24.0
  • Field Details

  • Constructor Details

    • Builder

      private Builder()
  • Method Details

    • withIgnoredFields

      public RecursiveAssertionConfiguration.Builder withIgnoredFields(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 class AbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
      Parameters:
      fieldsToIgnore - the fields to ignore in the object under test.
      Returns:
      this builder.
    • withIgnoredFieldsMatchingRegexes

      public RecursiveAssertionConfiguration.Builder withIgnoredFieldsMatchingRegexes(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 class AbstractRecursiveOperationConfiguration.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 the Predicate 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 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.

      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 field s but not to the internal fields of String. 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 to false.
      Returns:
      This builder.
    • withIgnoredFieldsOfTypes

      public RecursiveAssertionConfiguration.Builder withIgnoredFieldsOfTypes(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 class AbstractRecursiveOperationConfiguration.AbstractBuilder<RecursiveAssertionConfiguration.Builder>
      Parameters:
      types - the types we want to ignore in the object under test fields.
      Returns:
      This builder.
    • withCollectionAssertionPolicy

      Selects the RecursiveAssertionConfiguration.CollectionAssertionPolicy to use for recursive application of a Predicate to an object tree. If not set, defaults to RecursiveAssertionConfiguration.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

      Selects the RecursiveAssertionConfiguration.MapAssertionPolicy to use for recursive application of a Predicate to an object tree. If not set, defaults to RecursiveAssertionConfiguration.MapAssertionPolicy.MAP_OBJECT_AND_ENTRIES.

      Parameters:
      policy - The policy to use for maps in recursive asserting.
      Returns:
      This builder.
    • withOptionalAssertionPolicy

      Parameters:
      policy - the RecursiveAssertionConfiguration.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 - the RecursiveAssertionIntrospectionStrategy to use
      Returns:
      This builder.
    • build