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

public Class HamcrestCondition<T>

extends Condition<T>
Class Inheritance
Imports
org.hamcrest.Description, .Matcher, .StringDescription

Allows to use a Hamcrest matcher as a condition. Example:
 Condition<String> aStringContainingA = new HamcrestCondition<>(containsString("a"));

// assertions will pass
assertThat("abc").is(aStringContainingA);
assertThat("bc").isNot(aStringContainingA);

// assertion will fail
assertThat("bc").is(aStringContainingA);
By static-importing the matching(Matcher) method you can do:
 assertThat("abc").is(matching(containsString("a")));
Since
2.9.0 / 3.9.0

Field Summary

Modifier and TypeField and Description
private Matcher<? extends T>
Inherited from org.assertj.core.api.Condition:
description

Constructor Summary

AccessConstructor and Description
public
HamcrestCondition(Matcher<? extends T>
the Hamcrest matcher to use as a condition
matcher
)

Constructs a Condition using the matcher given as a parameter.

Method Summary

Modifier and TypeMethod and Description
private String
public boolean
matches(T
the value to verify.
value
)

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

Verifies that the given value satisfies this condition.
public static <
the type the condition is about
T
>
HamcrestCondition<T>

Returns:

the built HamcrestCondition
matching
(Matcher<? extends T>
the Hamcrest matcher to use as a condition
matcher
)

Constructs a Condition using the matcher given as a parameter.

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

Field Detail

matcherback to summary
private Matcher<? extends T> matcher

Constructor Detail

HamcrestConditionback to summary
public HamcrestCondition(Matcher<? extends T> matcher)

Constructs a Condition using the matcher given as a parameter.

Parameters
matcher:Matcher<? extends T>

the Hamcrest matcher to use as a condition

Method Detail

describeMatcherback to summary
private String describeMatcher()
matchesback to summary
public boolean matches(T value)

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

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

Verifies that the given value satisfies this condition.

Parameters
value:T

the value to verify.

Returns:boolean

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

Annotations
@Override
matchingback to summary
public static <T> HamcrestCondition<T> matching(Matcher<? extends T> matcher)

Constructs a Condition using the matcher given as a parameter.

Example:

 import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.HamcrestCondition.matching;
import static org.hamcrest.core.StringContains.containsString;

assertThat("abc").is(matching(containsString("a")));
Parameters
<T>
the type the condition is about
matcher:Matcher<? extends T>

the Hamcrest matcher to use as a condition

Returns:HamcrestCondition<T>

the built HamcrestCondition