Class NestableCondition<ACTUAL,NESTED>

java.lang.Object
org.assertj.core.api.Condition<ACTUAL>
org.assertj.core.condition.Join<ACTUAL>
org.assertj.core.condition.NestableCondition<ACTUAL,NESTED>
Type Parameters:
ACTUAL - the type of object this condition accepts (Customer in the example)
NESTED - the type of object nested into ACTUAL (Address in the example)
All Implemented Interfaces:
Descriptable<Condition<ACTUAL>>

public class NestableCondition<ACTUAL,NESTED> extends Join<ACTUAL>
Building block to define precise conditions on complex objects.

It allows to create readable assertions and produces beautiful assertion error messages.

Example:

 import static org.assertj.core.condition.NestableCondition.nestable;
import static org.assertj.core.condition.VerboseCondition.verboseCondition;

class Customer {
   final String name;
   final Address address;

   Customer(String name, Address address) {
     this.name = name;
     this.address = address;
   }
}

class Address {
   final String firstLine;
   final String postcode;

   Address(String firstLine, String postcode) {
     this.firstLine = firstLine;
     this.postcode = postcode;
   }
 }

static Condition<Customer> name(String expected) {
  return new Condition<>(
     it -> expected.equals(it.name),
     "name: " + expected
  );
}

static Condition<Customer> customer(Condition<Customer>... conditions) {
  return nestable("person", conditions);
}

static Condition<Address> firstLine(String expected) {
  return new Condition<>(
     it -> expected.equals(it.firstLine),
     "first line: " + expected
  );
}

static Condition<Address> postcode(String expected) {
  return new Condition<>(
     it -> expected.equals(it.postcode),
     "postcode: " + expected
  );
}

static Condition<Customer> address(Condition<Address>... conditions) {
  return nestable(
     "address",
     customer -> customer.address,
     conditions
  );
}
And assertions can be written like:
 assertThat(customer).is(
  customer(
    name("John"),
    address(
      firstLine("3"),
      postcode("KM3 8SP")
    )
  )
); 
leads to an easy-to-read assertion error:
 Expecting actual:
  org.assertj.core.condition.Customer@27ff5d15
to be:
  [✗] person:[
      [✓] name: John,
      [✗] address:[
        [✗] first line: 3,
        [✓] postcode: KM3 8SP
      ]
  ]
For an even better assertion error, see VerboseCondition.
Author:
Alessandro Ciccimarra
  • Method Details

    • nestable

      @SafeVarargs public static <ACTUAL,NESTED> Condition<ACTUAL> nestable(String descriptionPrefix, Function<? super ACTUAL, ? extends NESTED> extractor, Condition<? super NESTED>... conditions)
      Creates a new NestableCondition
      Type Parameters:
      ACTUAL - the type of object the resulting condition accepts
      NESTED - the type of object nested into K
      Parameters:
      descriptionPrefix - the prefix to use to build the description
      extractor - a function to extract the nested object of type T from an object fo type K
      conditions - conditions to be checked
      Returns:
      the nestable condition
    • nestable

      @SafeVarargs public static <ACTUAL> Condition<ACTUAL> nestable(String descriptionPrefix, Condition<? super ACTUAL>... conditions)
      Creates a new NestableCondition
      Type Parameters:
      ACTUAL - the type of object the resulting condition accepts
      Parameters:
      descriptionPrefix - the prefix to use to build the description
      conditions - conditions to be checked
      Returns:
      the nestable condition
    • matches

      public boolean matches(ACTUAL value)
      Description copied from class: Condition
      Verifies that the given value satisfies this condition.
      Overrides:
      matches in class Condition<ACTUAL>
      Parameters:
      value - the value to verify.
      Returns:
      true if the given value satisfies this condition; false otherwise.
    • descriptionPrefix

      public String descriptionPrefix()
      Description copied from class: Join
      method used to prefix the subclass join description, ex: "all of"
      Specified by:
      descriptionPrefix in class Join<ACTUAL>
      Returns:
      the prefix to use to build the description.