Key implements IKey { @Override public int hashCode() { return identifier != null ? identifier.hashCode() : 0; } Key(final String identifier); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testHashcode_isConsistent() { int initialHashcode = Fixture.x.hashCode(); assertEquals("Consistent hashcode test fails", initialHashcode, Fixture.x.hashCode()); int initialHashcodeInt = Fixture.xInt.hashCode(); assertEquals("Consistent hashcode test fails", initialHashcodeInt, Fixture.xInt.hashCode()); }
@Test public void testHashcode_twoEqualsObjects_produceSameNumber() { int xHashcode = Fixture.x.hashCode(); int yHashcode = Fixture.y.hashCode(); assertEquals("Equal object, return equal hashcode test fails", xHashcode, yHashcode); int xHashcodeInt = Fixture.xInt.hashCode(); int yHashcodeInt = Fixture.yInt.hashCode(); assertEquals("Equal object, return equal hashcode test fails", xHashcodeInt, yHashcodeInt); }
@Test public void testHashcode_twoUnEqualObjects_produceDifferentNumber() { int xHashcode = Fixture.x.hashCode(); int notxHashcode = Fixture.notx.hashCode(); assertTrue("Equal object, return unequal hashcode test fails", !(xHashcode == notxHashcode)); int xHashcodeInt = Fixture.xInt.hashCode(); int notxHashcodeInt = Fixture.notxInt.hashCode(); assertTrue("Equal object, return unequal hashcode test fails", !(xHashcodeInt == notxHashcodeInt)); }