Class VerboseCondition<T>
- java.lang.Object
-
- org.assertj.core.api.Condition<T>
-
- org.assertj.core.condition.VerboseCondition<T>
-
- Type Parameters:
T- the type of object the given condition accept.
- All Implemented Interfaces:
Descriptable<Condition<T>>
public final class VerboseCondition<T> extends Condition<T>
Conditionthat shows the value under test when the condition fails thanks to the specifiedobjectUnderTestDescriptorfunction.When defining the
objectUnderTestDescriptorfunction, you should take in consideration whether the condition is going to be used withis(Condition)orhas(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):
If we execute: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));
it fails with the following assertion error:assertThat("foooo").is(shorterThan4);Expecting actual: "foooo" to be shorter than 4 but length was 5Note that the beginning of the error message looks nice with
is(Condition), but not so much withhas(Condition):Expecting actual: "foooo" to have shorter than 4 but length was 5The
objectUnderTestDescriptormust not be null, if you don't need one this probably means you can simply useCondition(Predicate, String, Object...)instead of aVerboseCondition.- Author:
- Stefan Bischof
-
-
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 Stringdescriptionprivate Function<T,String>objectUnderTestDescriptor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected StringbuildVerboseDescription(T objectUnderTest, boolean matches)Build the verbose condition description when applied with the actual values and the match result.booleanmatches(T objectUnderTest)Verifies that the given value satisfies this condition.static <T> VerboseCondition<T>verboseCondition(Predicate<T> predicate, String description, Function<T,String> objectUnderTestDescriptor)Creates a newto have better control over the condition description when the condition fails thanks to theVerboseConditionobjectUnderTestDescriptorfunction parameter.-
Methods inherited from class org.assertj.core.api.Condition
conditionDescriptionWithStatus, describedAs, description, 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
-
verboseCondition
public static <T> VerboseCondition<T> verboseCondition(Predicate<T> predicate, String description, Function<T,String> objectUnderTestDescriptor)
Creates a newto have better control over the condition description when the condition fails thanks to theVerboseConditionobjectUnderTestDescriptorfunction parameter.When defining the
objectUnderTestDescriptorfunction, you should take in consideration whether the condition is going to be used withis(Condition)orhas(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):
If we execute: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));
it fails with the following assertion error:assertThat("foooo").is(shorterThan4);Expecting actual: "foooo" to be shorter than 4 but length was 5Note that the beginning of the error message looks nice with
is(Condition), but not so much withhas(Condition):Expecting actual: "foooo" to have shorter than 4 but length was 5The
objectUnderTestDescriptormust not be null, if you don't need one this probably means you can simply useCondition(Predicate, String, Object...)instead of aVerboseCondition.- Type Parameters:
T- the type of object the given condition accept.- Parameters:
predicate- the Predicate that tests the value to test.description- describes the Condition verbal.objectUnderTestDescriptor- Function used to describe the value to test when the actual value does not match the predicate. must not be null.- Returns:
- the created
VerboseCondition. - Throws:
NullPointerException- if the predicate isnull.NullPointerException- if the objectUnderTestDescriptor isnull.
-
matches
public boolean matches(T objectUnderTest)
Description copied from class:ConditionVerifies that the given value satisfies this condition.
-
buildVerboseDescription
protected String buildVerboseDescription(T objectUnderTest, boolean matches)
Build the verbose condition description when applied with the actual values and the match result.- Parameters:
objectUnderTest- the object to testmatches- the result of the match operation- Returns:
- the verbose condition description.
-
-