IsSameHashcode extends BaseMatcher<Object> { @Override public boolean matches(final Object arg) { checkNotNull(arg); return arg.hashCode() == object.hashCode(); } IsSameHashcode(final Object object); static Matcher sameHashcode(final Object target); @Override boolean matches(final Object arg); @Override void describeTo(final Description description);  }
@Test public void testGivenTwoInstancesOfSameObjectWhenMatchesIsCalledThenTrueIsReturned() throws Exception { assumeTrue("Assumed that actual and comparison have the same hashcode", actual.hashCode() == comparison.hashCode()); final boolean matches = sameHashcodeMatcher.matches(actual); assertMatches(matches); }
@Test public void testGivenTwoInstancesOfDifferentObjectsWhenMatchesIsCalledThenFalseIsReturned() throws Exception { assumeFalse("Assumed that actual and comparison have different haschode", anotherActual.hashCode() == comparison.hashCode()); final boolean matches = sameHashcodeMatcher.matches(anotherActual); assertDoesNotMatch(matches); }