ConjunctionMatcher extends TypeSafeDiagnosingMatcher<T> { public ConjunctionMatcher<T> and(Matcher<? super T> matcher) { requireNonNull(matcher, "matcher"); return new ConjunctionMatcher<>(compositeDescription, concat(matchers, matcher)); } ConjunctionMatcher(String compositeDescription, Iterable<Matcher<? super T>> matchers); ConjunctionMatcher<T> and(Matcher<? super T> matcher); @Override void describeTo(Description description);  }
@Test public void andReturnsCompositeMatcher() { ConjunctionMatcher<Object> matcher = compose("x", anything("y")); ConjunctionMatcher<Object> actual = matcher.and(anything("z")); assertThat(asString(actual), is("x y\n" + " and z")); }
@Test public void andWithSupertypeMatcherReturnsCompositeMatcher() { ConjunctionMatcher<String> matcher = compose("x", anything("y")); ConjunctionMatcher<String> actual = matcher.and(anything("z")); assertThat(asString(actual), is("x y\n" + " and z")); }
@Test public void andPreservesMatcher() { ConjunctionMatcher<Object> matcher = compose("x", anything("y")); matcher.and(anything()); assertThat(asString(matcher), is("x y")); }
@Test(expected = NullPointerException.class) public void andWithNullMatcherThrowsException() { ConjunctionMatcher<Object> matcher = compose(anything()); matcher.and(null); }
@Test public void matchesWhenMatchersMatchReturnsTrue() { ConjunctionMatcher<String> matcher = compose(startsWith("x")).and(endsWith("y")); assertThat(matcher.matches("xy"), is(true)); }
@Test public void matchesWhenFirstMatcherDoesNotMatchReturnsFalse() { ConjunctionMatcher<String> matcher = compose(startsWith("x")).and(endsWith("y")); assertThat(matcher.matches("zy"), is(false)); }
@Test public void matchesWhenSecondMatcherDoesNotMatchReturnsFalse() { ConjunctionMatcher<String> matcher = compose(startsWith("x")).and(endsWith("y")); assertThat(matcher.matches("xz"), is(false)); }
@Test public void matchesWhenMatchersDoNotMatchReturnsFalse() { ConjunctionMatcher<String> matcher = compose(startsWith("x")).and(endsWith("y")); assertThat(matcher.matches("z"), is(false)); }
@Test public void describeMismatchWhenFirstMatcherDoesNotMatchDescribesMismatch() { ConjunctionMatcher<Object> matcher = compose(nothing("x")).and(anything()); StringDescription description = new StringDescription(); matcher.describeMismatch("y", description); assertThat(description.toString(), is("x was \"y\"")); }
@Test public void describeMismatchWhenSecondMatcherDoesNotMatchDescribesMismatch() { ConjunctionMatcher<Object> matcher = compose(anything()).and(nothing("x")); StringDescription description = new StringDescription(); matcher.describeMismatch("y", description); assertThat(description.toString(), is("x was \"y\"")); }
@Test public void describeMismatchWhenMatchersDoNotMatchDescribesMismatches() { ConjunctionMatcher<Object> matcher = compose(nothing("x")).and(nothing("y")); StringDescription description = new StringDescription(); matcher.describeMismatch("z", description); assertThat(description.toString(), is("x was \"z\"\n" + " and y was \"z\"")); }