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>>
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
-
Nested Class Summary
Nested classes/interfaces inherited from class org.assertj.core.api.Condition
Condition.Status
-
Field Summary
Fields inherited from class org.assertj.core.condition.Join
PREFIX_DELIMITER, SUFFIX_DELIMITER
-
Method Summary
Modifier and TypeMethodDescriptionmethod used to prefix the subclass join description, ex: "all of"boolean
Verifies that the given value satisfies this condition.static <ACTUAL,
NESTED>
Condition<ACTUAL>nestable
(String descriptionPrefix, Function<ACTUAL, NESTED> extractor, Condition<NESTED>... conditions) Creates a newNestableCondition
static <ACTUAL> Condition<ACTUAL>
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 Details
-
nestable
@SafeVarargs public static <ACTUAL,NESTED> Condition<ACTUAL> nestable(String descriptionPrefix, 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(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
Description copied from class:Condition
Verifies that the given value satisfies this condition. -
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.
-