Package org.assertj.core.condition
Class MappedCondition<FROM,TO>
- java.lang.Object
-
- org.assertj.core.api.Condition<FROM>
-
- org.assertj.core.condition.MappedCondition<FROM,TO>
-
- Type Parameters:
FROM
- the type of object this condition accepts.TO
- the type of object the nested condition accepts.
- All Implemented Interfaces:
Descriptable<Condition<FROM>>
public class MappedCondition<FROM,TO> extends Condition<FROM>
ContainerCondition
that maps the object under test and then check the resulting mapped value against its nestedCondition
.Example:
Condition<String> hasLineSeparator = new Condition<>(t -> t.contains(System.lineSeparator()), "has lineSeparator"); Condition<Optional<String>> optionalWithLineSeparator = MappedCondition.mappedCondition(Optional::get, hasLineSeparator, "optional value has lineSeparator"); // assertion succeeds assertThat(Optional.of("a" + System.lineSeparator())).is(optionalWithLineSeparator); // returns true optionalWithLineSeparator.matches(Optional.of("a" + System.lineSeparator())); // assertion fails assertThat(Optional.of("a")).is(optionalWithLineSeparator); // returns false optionalWithLineSeparator.matches(Optional.of("a"));
- Author:
- Stefan Bischof
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.assertj.core.api.Condition
Condition.Status
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected String
buildMappingDescription(FROM from, TO to)
Build the mapped condition description when applied with the FROM and TO values.private String
buildMappingDescription(FROM from, TO to, boolean withNested)
private static String
className(Object object)
Description
conditionDescriptionWithStatus(FROM actual)
Returns the description of this condition with its status failed or success.static <FROM,TO>
MappedCondition<FROM,TO>mappedCondition(Function<FROM,TO> mapping, Condition<TO> condition)
Creates a newMappedCondition
static <FROM,TO>
MappedCondition<FROM,TO>mappedCondition(Function<FROM,TO> mapping, Condition<TO> condition, String mappingDescription, Object... args)
Creates a new
.MappedCondition
boolean
matches(FROM value)
Maps the value with the given function and verifies that it satisfies the nested
.Condition
-
Methods inherited from class org.assertj.core.api.Condition
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
-
mappedCondition
public static <FROM,TO> MappedCondition<FROM,TO> mappedCondition(Function<FROM,TO> mapping, Condition<TO> condition, String mappingDescription, Object... args)
Creates a new
.MappedCondition
Example:
Condition<String> hasLineSeparator = new Condition<>(t -> t.contains(System.lineSeparator()), "has lineSeparator"); Condition<Optional<String>> optionalWithLineSeparator = MappedCondition.mappedCondition(Optional::get, hasLineSeparator, "optional value has lineSeparator"); // assertion succeeds assertThat(Optional.of("a" + System.lineSeparator())).is(optionalWithLineSeparator); // returns true optionalWithLineSeparator.matches(Optional.of("a" + System.lineSeparator())); // assertion fails assertThat(Optional.of("a")).is(optionalWithLineSeparator); // returns false optionalWithLineSeparator.matches(Optional.of("a"));
Note that the mappingDescription argument follows
String.format(String, Object...)
syntax.- Type Parameters:
FROM
- the type of object the given condition accept.TO
- the type of object the nested condition accept.- Parameters:
mapping
- the Function that maps the value to test to the a value for the nested condition.condition
- the nested condition to evaluate.mappingDescription
- describes the mapping, followsString.format(String, Object...)
syntax.args
- for describing the mapping as inString.format(String, Object...)
syntax.- Returns:
- the created
MappedCondition
. - Throws:
NullPointerException
- if the given condition isnull
.NullPointerException
- if the given mapping isnull
.
-
mappedCondition
public static <FROM,TO> MappedCondition<FROM,TO> mappedCondition(Function<FROM,TO> mapping, Condition<TO> condition)
Creates a newMappedCondition
- Type Parameters:
FROM
- the type of object the given condition accept.TO
- the type of object the nested condition accept.- Parameters:
mapping
- the Function that maps the value to test to the a value for the nested condition.condition
- the nested condition to evaluate.- Returns:
- the created
MappedCondition
. - Throws:
NullPointerException
- if the given condition isnull
.NullPointerException
- if the given mapping isnull
.
-
matches
public boolean matches(FROM value)
Maps the value with the given function and verifies that it satisfies the nested
.Condition
-
buildMappingDescription
protected String buildMappingDescription(FROM from, TO to)
Build the mapped condition description when applied with the FROM and TO values.- Parameters:
from
- the value to mapto
- the mapped value- Returns:
- the mapped condition description .
-
buildMappingDescription
private String buildMappingDescription(FROM from, TO to, boolean withNested)
-
conditionDescriptionWithStatus
public Description conditionDescriptionWithStatus(FROM actual)
Description copied from class:Condition
Returns the description of this condition with its status failed or success.- Overrides:
conditionDescriptionWithStatus
in classCondition<FROM>
- Parameters:
actual
- the instance to evaluate the condition status against.- Returns:
- the description of this condition with its status.
-
-