Class Matchers
Matchers.- Since:
- 1.1
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> org.hamcrest.Matcher<T> Returns aMatcherthat matches againstnullorvoid.static <T> org.hamcrest.Matcher<T> deepEquals(T expected) Constructs a deep equalsMatcher.static <T> org.hamcrest.Matcher<T> deepEquals(T expected, FieldFilter filter) Constructs a deep equalsMatcher.static <T> org.hamcrest.Matcher<T> exactClassOf(Class<T> expected) Returns aMatcherthat matches with exact class type defined by the givenexpected.static <T> org.hamcrest.Matcher<List<T>> exactSequenceOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a list of items of typeTif each of the givenmatchersmatches against the item at the respective index in the list.static <T> org.hamcrest.Matcher<List<T>> listWithAllOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a List where all the given matchers must match with at least one of the items in that list.static <T> org.hamcrest.Matcher<List<T>> listWithAnyOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a list of items of typeTwhere at least one of the givenmatchersmatches any of the items in that list.static <T> org.hamcrest.Matcher<T> Returns aMatcherthat matches with values of typeTdefined by the givenpredicate.static <T extends Message>
org.hamcrest.Matcher<T> messageWithPayload(org.hamcrest.Matcher<?> payloadMatcher) Returns aMatcherthat matches a singleMessageif the givenpayloadMatchermatches that message's payload.static org.hamcrest.Matcher<List<CommandMessage>> static org.hamcrest.Matcher<List<EventMessage>> noEvents()static <T> org.hamcrest.Matcher<T> nothing()Returns aMatcherthat matches againstnullorvoid.payloadsMatching(org.hamcrest.Matcher<? extends List<?>> matcher) Returns aMatcherthat matches a list ofMessageif a list containing their respective payloads matches the givenmatcher.static <T> org.hamcrest.Matcher<T> Returns aMatcherthat matches with values of typeTdefined by the givenpredicate.static <T> org.hamcrest.Matcher<List<T>> sequenceOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a list of items of typeTif each of thematchersmatch against an item that comes after the item that the previous matcher matched against.
-
Method Details
-
payloadsMatching
public static org.hamcrest.Matcher<List<? extends Message>> payloadsMatching(org.hamcrest.Matcher<? extends List<?>> matcher) Returns aMatcherthat matches a list ofMessageif a list containing their respective payloads matches the givenmatcher.- Parameters:
matcher- The matcher to match againstMessage's payload.- Returns:
- A matcher that matches against the
Message payloads.
-
messageWithPayload
public static <T extends Message> org.hamcrest.Matcher<T> messageWithPayload(org.hamcrest.Matcher<?> payloadMatcher) Returns aMatcherthat matches a singleMessageif the givenpayloadMatchermatches that message's payload.- Type Parameters:
T- The generic type ofMessagematched on by the resulting payload matcher.- Parameters:
payloadMatcher- The matcher to match against theMessage's payload.- Returns:
- A matcher that evaluates a the
Message payload.
-
listWithAllOf
@SafeVarargs public static <T> org.hamcrest.Matcher<List<T>> listWithAllOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a List where all the given matchers must match with at least one of the items in that list.- Type Parameters:
T- The generic item type matched by the givenmatchers.- Parameters:
matchers- the matchers that should match against one of the items in the List.- Returns:
- A matcher that matches a number of matchers against a list.
-
listWithAnyOf
@SafeVarargs public static <T> org.hamcrest.Matcher<List<T>> listWithAnyOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a list of items of typeTwhere at least one of the givenmatchersmatches any of the items in that list.- Type Parameters:
T- The generic item type matched by the givenmatchers.- Parameters:
matchers- The matchers that should match against one of the items of typeTin the list.- Returns:
- A matcher that matches a number of generic-type-matchers against a list of objects.
-
sequenceOf
@SafeVarargs public static <T> org.hamcrest.Matcher<List<T>> sequenceOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a list of items of typeTif each of thematchersmatch against an item that comes after the item that the previous matcher matched against.This means that the given
matchersmust match in order, but there may be "gaps" of unmatched items in between. To match the exact sequence of items (i.e. without gaps), useexactSequenceOf(org.hamcrest.Matcher[]).- Type Parameters:
T- The generic item type matched by the givenmatchers.- Parameters:
matchers- The matchers to match against the list of items of typeT.- Returns:
- A matcher that matches a number of item-matchers against a list of items of type
T.
-
exactSequenceOf
@SafeVarargs public static <T> org.hamcrest.Matcher<List<T>> exactSequenceOf(org.hamcrest.Matcher<T>... matchers) Returns aMatcherthat matches a list of items of typeTif each of the givenmatchersmatches against the item at the respective index in the list.This means the first matcher must match the first item, the second matcher the second item, and so on. Any excess items are ignored. If there are excess
To allow "gaps" of unmatched items, useMatchers, they will be evaluated againstnull. To make sure the number of items matches the number ofMatchers, you can append an extraandNoMore()matcher.sequenceOf(org.hamcrest.Matcher[])instead.- Type Parameters:
T- The generic item type matched by the givenmatchers.- Parameters:
matchers- The matchers to match against the list of items of typeT.- Returns:
- A matcher that matches a number of item-matchers against a list of items of type
T.
-
matches
Returns aMatcherthat matches with values of typeTdefined by the givenpredicate.This method is a synonym for
predicate(Predicate)to allow for better readability.- Type Parameters:
T- The generic item type verified by the givenpredicate.- Parameters:
predicate- The predicate defining matching values.- Returns:
- A matcher that matches against values of type
Tdefined by the predicate.
-
predicate
Returns aMatcherthat matches with values of typeTdefined by the givenpredicate.This method is a synonym for
matches(Predicate)to allow for better readability.- Type Parameters:
T- The generic item type verified by the givenpredicate.- Parameters:
predicate- The predicate defining matching values- Returns:
- A matcher that matches against values of type
Tdefined by the predicate.
-
noEvents
-
noCommands
-
exactClassOf
Returns aMatcherthat matches with exact class type defined by the givenexpected.- Type Parameters:
T- The object type to match the givenexpectedclass with.- Parameters:
expected- The expected class.- Returns:
- A matcher that matches based on the
expectedclass.
-
deepEquals
public static <T> org.hamcrest.Matcher<T> deepEquals(T expected) Constructs a deep equalsMatcher.This
Matcherwill first perform a regular equals check based on the givenexpectedandactual(provided during theMatcher.matches(Object)invocation). If this fails and given type does not overrideObject.equals(Object), thisMatcherwill match the fields of the givenexpectedandactual.- Type Parameters:
T- The type of theexpectedobject to match against.- Parameters:
expected- The object to match against.- Returns:
- A matcher matching on
Object.equals(Object)firstly, followed by field value equality if equals isn't implemented.
-
deepEquals
Constructs a deep equalsMatcher.This
Matcherwill first perform a regular equals check based on the givenexpectedandactual(provided during theMatcher.matches(Object)invocation). If this fails and given type does not overrideObject.equals(Object), thisMatcherwill match the fields of the givenexpectedandactual. Fields can be in- or excluded for this last step through thefilter.- Type Parameters:
T- The type of theexpectedobject to match against.- Parameters:
expected- The object to match against.filter- The filter describing the Fields to include in the comparison.- Returns:
- A matcher matching on
Object.equals(Object)firstly, followed by field value equality if equals isn't implemented.
-
andNoMore
public static <T> org.hamcrest.Matcher<T> andNoMore()Returns aMatcherthat matches againstnullorvoid.Can be used to make sure no trailing items remain when using an
exact sequence matcher.- Type Parameters:
T- The type of the object to match against.- Returns:
- A matcher that matches against "nothing".
-
nothing
public static <T> org.hamcrest.Matcher<T> nothing()Returns aMatcherthat matches againstnullorvoid.Can be used to make sure no trailing items remain when using an
exact sequence matcher.- Type Parameters:
T- The type of the object to match against.- Returns:
- A matcher that matches against "nothing".
-