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.
| Modifier and Type | Field and Description |
|---|---|
| private final String |
| Access | Constructor and Description |
|---|---|
| private | NestableCondition(String descriptionPrefix, Stream<? extends Condition<? super NESTED>> conditions, Function<? super ACTUAL, ? extends NESTED> extractor)
|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static <ACTUAL, NESTED> List | |
| private static <ACTUAL, NESTED> Condition | |
| public String | descriptionPrefix()
Implements abstract org. |
| public boolean | matches(ACTUAL
the value to verify. value)Overrides org. |
| public static < the type of object the resulting condition accepts ACTUAL, the type of object nested into K NESTED> Condition | Returns: the nestable conditionthe prefix to use to build the description descriptionPrefix, Function<? super ACTUAL, ? extends NESTED> a function to extract the nested object of type T from an object fo type K extractor, Condition<? super NESTED>... conditions to be checked conditions)Creates a new |
| public static < the type of object the resulting condition accepts ACTUAL> Condition | Returns: the nestable conditionthe prefix to use to build the description descriptionPrefix, Condition<? super ACTUAL>... conditions to be checked conditions)Creates a new |
| descriptionPrefix | back to summary |
|---|---|
| private final String descriptionPrefix | |
| NestableCondition | back to summary |
|---|---|
| private NestableCondition(String descriptionPrefix, Stream<? extends Condition<? super NESTED>> conditions, Function<? super ACTUAL, ? extends NESTED> extractor) | |
| NestableCondition | back to summary |
|---|---|
| private NestableCondition(String descriptionPrefix, Stream<? extends Condition<? super ACTUAL>> conditions) | |
| compose | back to summary |
|---|---|
| private static <ACTUAL, NESTED> List | |
| compose | back to summary |
|---|---|
| private static <ACTUAL, NESTED> Condition | |
| descriptionPrefix | back to summary |
|---|---|
| public String descriptionPrefix() Implements abstract org. Doc from org. method used to prefix the subclass join description, ex: "all of" | |
| matches | back to summary |
|---|---|
| public boolean matches(ACTUAL value) Overrides org. Doc from org. Verifies that the given value satisfies this condition.
| |
| nestable | back to summary |
|---|---|
| public static <ACTUAL, NESTED> Condition Creates a new
| |
| nestable | back to summary |
|---|---|
| public static <ACTUAL> Condition Creates a new
| |