Package org.assertj.core.condition
Class NestableCondition<ACTUAL,NESTED>
- java.lang.Object
-
- org.assertj.core.api.Condition<T>
-
- 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:
And assertions can be written like: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 ); }
leads to an easy-to-read assertion error:assertThat(customer).is( customer( name("John"), address( firstLine("3"), postcode("KM3 8SP") ) ) );
For an even better assertion error, seeExpecting actual: org.assertj.core.condition.Customer@27ff5d15 to be: [✗] person:[ [✓] name: John, [✗] address:[ [✗] first line: 3, [✓] postcode: KM3 8SP ] ]
.VerboseCondition
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.assertj.core.api.Condition
Condition.Status
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
descriptionPrefix
-
Fields inherited from class org.assertj.core.condition.Join
conditions, PREFIX_DELIMITER, SUFFIX_DELIMITER
-
-
Constructor Summary
Constructors Modifier Constructor Description private
NestableCondition(java.lang.String descriptionPrefix, java.util.stream.Stream<Condition<ACTUAL>> conditions)
private
NestableCondition(java.lang.String descriptionPrefix, java.util.stream.Stream<Condition<NESTED>> conditions, java.util.function.Function<ACTUAL,NESTED> extractor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static <ACTUAL,NESTED>
java.util.List<Condition<ACTUAL>>compose(java.util.stream.Stream<Condition<NESTED>> conditions, java.util.function.Function<ACTUAL,NESTED> extractor)
private static <ACTUAL,NESTED>
Condition<ACTUAL>compose(Condition<NESTED> condition, java.util.function.Function<ACTUAL,NESTED> extractor)
java.lang.String
descriptionPrefix()
method used to prefix the subclass join description, ex: "all of"boolean
matches(ACTUAL value)
Verifies that the given value satisfies this condition.static <ACTUAL,NESTED>
Condition<ACTUAL>nestable(java.lang.String descriptionPrefix, java.util.function.Function<ACTUAL,NESTED> extractor, Condition<NESTED>... conditions)
Creates a newNestableCondition
static <ACTUAL> Condition<ACTUAL>
nestable(java.lang.String descriptionPrefix, Condition<ACTUAL>... conditions)
Creates a newNestableCondition
-
Methods inherited from class org.assertj.core.condition.Join
conditionDescriptionWithStatus, conditions, description
-
Methods inherited from class org.assertj.core.api.Condition
describedAs, status, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.Descriptable
as, as, as, describedAs
-
-
-
-
Method Detail
-
nestable
@SafeVarargs public static <ACTUAL,NESTED> Condition<ACTUAL> nestable(java.lang.String descriptionPrefix, java.util.function.Function<ACTUAL,NESTED> extractor, Condition<NESTED>... conditions)
Creates a newNestableCondition
- Type Parameters:
ACTUAL
- the type of object the resulting condition acceptsNESTED
- the type of object nested into K- Parameters:
descriptionPrefix
- the prefix to use to build the descriptionextractor
- a function to extract the nested object of type T from an object fo type Kconditions
- conditions to be checked- Returns:
- the nestable condition
-
nestable
@SafeVarargs public static <ACTUAL> Condition<ACTUAL> nestable(java.lang.String descriptionPrefix, Condition<ACTUAL>... conditions)
Creates a newNestableCondition
- Type Parameters:
ACTUAL
- the type of object the resulting condition accepts- Parameters:
descriptionPrefix
- the prefix to use to build the descriptionconditions
- 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.
-
descriptionPrefix
public java.lang.String descriptionPrefix()
Description copied from class:Join
method used to prefix the subclass join description, ex: "all of"- Specified by:
descriptionPrefix
in classJoin<ACTUAL>
- Returns:
- the prefix to use to build the description.
-
compose
private static <ACTUAL,NESTED> java.util.List<Condition<ACTUAL>> compose(java.util.stream.Stream<Condition<NESTED>> conditions, java.util.function.Function<ACTUAL,NESTED> extractor)
-
-