IsIterableContaining extends TypeSafeDiagnosingMatcher<Iterable<? super T>> { public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) { return new IsIterableContaining<>(itemMatcher); } IsIterableContaining(Matcher<? super T> elementMatcher); @Override void describeTo(Description description); static Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher); static Matcher<Iterable<? super T>> hasItem(T item); @SafeVarargs static Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers); @SafeVarargs static Matcher<Iterable<T>> hasItems(T... items);  }
@Test public void copesWithNullsAndUnknownTypes() { Matcher<?> matcher = hasItem(equalTo("irrelevant")); assertNullSafe(matcher); assertUnknownTypeSafe(matcher); }
@Test public void matchesACollectionThatContainsAnElementForTheGivenMatcher() { final Matcher<Iterable<? super String>> itemMatcher = hasItem(equalTo("a")); assertMatches("list containing 'a'", itemMatcher, asList("a", "b", "c")); }
@Test public void doesNotMatchCollectionWithoutAnElementForGivenMatcher() { final Matcher<Iterable<? super String>> matcher = hasItem(mismatchable("a")); assertMismatchDescription("mismatches were: [mismatched: b, mismatched: c]", matcher, asList("b", "c")); assertMismatchDescription("was empty", matcher, new ArrayList<String>()); }
@Test public void doesNotMatchNull() { assertDoesNotMatch("doesn't match null", hasItem(equalTo("a")), null); }
@Test public void hasAReadableDescription() { assertDescription("a collection containing mismatchable: a", hasItem(mismatchable("a"))); }
@Test public void canMatchItemWhenCollectionHoldsSuperclass() { final Set<Number> s = new HashSet<>(); s.add(2); assertMatches(new IsIterableContaining<>(new IsEqual<Number>(2)), s); assertMatches(IsIterableContaining.hasItem(2), s); }