I am trying to test if a collection has an item which toString() method returns a particular String. I tried do it using excellent Hamcrest matching classes, by combining co
I think you're looking for Matchers.hasItem(..)
Assert.assertThat(items, Matchers.hasItem(Matchers.hasToString("c")));
which states
Creates a matcher for Iterables that only matches when a single pass over the examined
Iterable
yields at least one item that is matched by the specifieditemMatcher
. Whilst matching, the traversal of the examinedIterable
will stop as soon as a matching item is found.
Matchers.contains
, as you stated,
Creates a matcher for Iterables that matches when a single pass over the examined
Iterable
yields a single item that satisfies the specified matcher. For a positive match, the examined iterable must only yield one item.
It seems to me like that's saying there should only be one element in the Iterable
.