public class HamcrestCondition<T> extends Condition<T>
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")));Condition.Status| Constructor and Description |
|---|
HamcrestCondition(org.hamcrest.Matcher<? extends T> matcher)
Constructs a
Condition using the matcher given as a parameter. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
matches(T value)
Verifies that the given value satisfies this condition.
|
static <T> HamcrestCondition<T> |
matching(org.hamcrest.Matcher<? extends T> matcher)
Constructs a
Condition using the matcher given as a parameter. |
conditionDescriptionWithStatus, describedAs, description, status, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitas, as, as, describedAspublic static <T> HamcrestCondition<T> matching(org.hamcrest.Matcher<? extends T> matcher)
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")));T - the type the condition is aboutmatcher - the Hamcrest matcher to use as a conditionHamcrestConditionCopyright © 2025. All rights reserved.