Top Description Fields Constructors Methods
org.assertj.core.condition

public final Class VerboseCondition<T>

extends Condition<T>
Class Inheritance
Annotations
@Beta
Type Parameters
<T>
the type of object the given condition accept.
Static Imports
java.lang.String.format, java.util.Objects.requireNonNull

Condition that shows the value under test when the condition fails thanks to the specified objectUnderTestDescriptor function.

When defining the objectUnderTestDescriptor function, you should take in consideration whether the condition is going to be used with is(Condition) or has(Condition) since the start of the error message is different between the two.

Let's see how it works with an example that works well with is(Condition):

 Condition<String> shorterThan4 = VerboseCondition.verboseCondition(actual -> actual.length() < 4,
                                                                  // predicate description  
                                                                  "shorter than 4",
                                                                  // value under test description transformation function
                                                                  s -> String.format(" but length was %s", s.length(), s));
If we execute:
 assertThat("foooo").is(shorterThan4);
it fails with the following assertion error:
 Expecting actual:
  "foooo"
to be shorter than 4 but length was 5

Note that the beginning of the error message looks nice with is(Condition), but not so much with has(Condition):

 Expecting actual:
  "foooo"
to have shorter than 4 but length was 5

The objectUnderTestDescriptor must not be null, if you don't need one this probably means you can simply use Condition#Condition(Predicate, String, Object...) instead of a VerboseCondition.

Author
Stefan Bischof

Field Summary

Modifier and TypeField and Description
private String
private Function<T, String>

Constructor Summary

AccessConstructor and Description
private
VerboseCondition(Predicate<T> predicate, String description, Function<T, String> objectUnderTestDescriptor)

Method Summary

Modifier and TypeMethod and Description
protected String

Returns:

the verbose condition description.
buildVerboseDescription
(T
the object to test
objectUnderTest
,
boolean
the result of the match operation
matches
)

Build the verbose condition description when applied with the actual values and the match result.

public boolean
matches(T
the value to verify.
objectUnderTest
)

Overrides org.assertj.core.api.Condition.matches.

Verifies that the given value satisfies this condition.
public static <
the type of object the given condition accept.
T
>
VerboseCondition<T>

Returns:

the created VerboseCondition.
verboseCondition
(Predicate<T>
the Predicate that tests the value to test.
predicate
,
String
describes the Condition verbal.
description
,
Function<T, String>
Function used to describe the value to test when the actual value does not match the predicate. must not be null.
objectUnderTestDescriptor
)

Creates a new VerboseCondition to have better control over the condition description when the condition fails thanks to the objectUnderTestDescriptor function parameter.

Inherited from org.assertj.core.api.Condition:
conditionDescriptionWithStatusdescribedAsdescriptionstatustoString

Field Detail

descriptionback to summary
private String description

Hides org.assertj.core.api.Condition.description.

objectUnderTestDescriptorback to summary
private Function<T, String> objectUnderTestDescriptor

Constructor Detail

VerboseConditionback to summary
private VerboseCondition(Predicate<T> predicate, String description, Function<T, String> objectUnderTestDescriptor)

Method Detail

buildVerboseDescriptionback to summary
protected String buildVerboseDescription(T objectUnderTest, boolean matches)

Build the verbose condition description when applied with the actual values and the match result.

Parameters
objectUnderTest:T

the object to test

matches:boolean

the result of the match operation

Returns:String

the verbose condition description.

matchesback to summary
public boolean matches(T objectUnderTest)

Overrides org.assertj.core.api.Condition.matches.

Doc from org.assertj.core.api.Condition.matches.

Verifies that the given value satisfies this condition.

Parameters
objectUnderTest:T

the value to verify.

Returns:boolean

true if the given value satisfies this condition; false otherwise.

Annotations
@Override
verboseConditionback to summary
public static <T> VerboseCondition<T> verboseCondition(Predicate<T> predicate, String description, Function<T, String> objectUnderTestDescriptor)

Creates a new VerboseCondition to have better control over the condition description when the condition fails thanks to the objectUnderTestDescriptor function parameter.

When defining the objectUnderTestDescriptor function, you should take in consideration whether the condition is going to be used with is(Condition) or has(Condition) since the start of the error message is different between the two.

Let's see how it works with an example that works well with is(Condition):

 Condition<String> shorterThan4 = VerboseCondition.verboseCondition(actual -> actual.length() < 4,
                                                                   // predicate description  
                                                                   "shorter than 4",
                                                                   // value under test description transformation function
                                                                   s -> String.format(" but length was %s", s.length(), s));
If we execute:
 assertThat("foooo").is(shorterThan4);
it fails with the following assertion error:
 Expecting actual:
  "foooo"
to be shorter than 4 but length was 5

Note that the beginning of the error message looks nice with is(Condition), but not so much with has(Condition):

 Expecting actual:
  "foooo"
to have shorter than 4 but length was 5

The objectUnderTestDescriptor must not be null, if you don't need one this probably means you can simply use Condition#Condition(Predicate, String, Object...) instead of a VerboseCondition.

Parameters
<T>
the type of object the given condition accept.
predicate:Predicate<T>

the Predicate that tests the value to test.

description:String

describes the Condition verbal.

objectUnderTestDescriptor:Function<T, String>

Function used to describe the value to test when the actual value does not match the predicate. must not be null.

Returns:VerboseCondition<T>

the created VerboseCondition.

Exceptions
NullPointerException:
  • if the predicate is null.
  • if the objectUnderTestDescriptor is null.