IsMapWithSameKeySet extends TypeSafeMatcher<Map<? extends K, ?>> { @Override public void describeMismatchSafely(final Map<? extends K, ?> map, final Description mismatchDescription) { mismatchDescription.appendText("map key set was ").appendValueList("[", ", ", "]", map.entrySet()); } IsMapWithSameKeySet(final Map<? extends K, ?> comparisonMap); static Matcher<Map<? extends K, ?>> hasSameKeySet(final Map<? extends K, ?> comparisonMap); @Override boolean matchesSafely(final Map<? extends K, ?> map); @Override void describeMismatchSafely(final Map<? extends K, ?> map, final Description mismatchDescription); @Override void describeTo(final Description description);  }
@Test public void testDescribeMismatchSafely() throws Exception { assertHasMismatchDescription("was null", isMapWithSameKeySetMatcher, null); assertHasMismatchDescription("map key set was []", isMapWithSameKeySetMatcher, ImmutableMap.<Integer, Object>of()); }