The input:
The goal is make following statement compilable:
Collection<MyElement> elements = ...
Collection<TypeSafeMatchert> matchers = ...
assertThat(elements, Matchers.contains(matchers); //<error here
What have to use here? It wants me to Matcher<? super java.util.List<MyElement>>
Matcher<? super java.util.List<MyElement>>
and told that actully I passed Matcher<java.lang.Iterable<? super java.util.List<MyElement>>>
Matcher<java.lang.Iterable<? super java.util.List<MyElement>>>
. So how to pass a Matcher Collection here?
There is a question about comparing collections with hamcrest, but there is no example with passing Matchers collection, not elements.
Use List instead of collection for the matchers, or convert it to array.
Hamcrest has following contains
methods:
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E>... itemMatchers)
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(E... items)
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E> itemMatcher)
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers)
As you can see it handles Matchers only in case of a List
or varags (but if you pass only one element then you need to convert it to array).
Instead of defining a Collection
of TypeSafeMatchers
, you need to define a:
List<Matcher<? super MyElement>> matchers = ...;
This way, Hamcrest
will know what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.