I'm using Hamcrest 1.3 and trying to achieve the following in a more compact way.
Consider following test case:
@Test
public void testCase() throws Exception {
Collection<String> strings = Arrays.asList(
"string one",
"string two",
"string three"
);
// variant 1:
assertThat(strings, hasSize(greaterThan(2)));
assertThat(strings, hasItem(is("string two")));
// variant 2:
assertThat(strings, allOf(
hasSize(greaterThan(2)),
hasItem(is("string two"))
));
}
the goal here is to check for both size of collection AND some specific items to be included.
Where first variation is possible and accepted, it is not always that easy to do it, because maybe the collection is itself a result of some other operations and therefore it makes more sense to do all the operations on it using an allOf
operation. Which is done in second variation above.
However containing the code of second variation will result in following compile time error:
error: no suitable method found for allOf(Matcher<Collection<? extends Object>>,Matcher<Iterable<? extends String>>)
Is there actually any specific way of testing for size AND items of a collection in Hamcrest using a single shot operation (like allOf
)?
java - jUnit testing using hamcrest matcher - how to test the size of a collection.
Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.
Class StringContains. Tests if the argument is a string that contains a substring. Creates a matcher that matches if the examined String contains the specified String anywhere.
I think the compiler is not able to sort out the generics. The following is working for me (JDK 8u102):
assertThat(strings, Matchers.<Collection<String>> allOf(
hasSize(greaterThan(2)),
hasItem(is("string two"))
));
I know this question is old but I still want to answer it in case someone needs explanation.
If you want to use allOf(...)
just make sure nested matchers return types match. In your test case hasSize
returns org.hamcrest.Matcher<java.util.Collection<? extends T>>
but hasItem
yields org.hamcrest.Matcher<java.lang.Iterable<? super T>>
.
In this case I'd recommend to use iterableWithSize(int size)
where return type is Matcher<java.lang.Iterable<T>>
, so you could do:
assertThat(strings,
allOf(
iterableWithSize(greaterThan(2)),
hasItem("string two")
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With