### Question:
SortingOption { @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((direction == null) ? 0 : direction.hashCode()); result = prime * result + ((key == null) ? 0 : key.hashCode()); return result; } SortingOption(final String key);  SortingOption(final String key, final Direction direction); String key(); Direction direction(); @Override int hashCode(); @Override boolean equals(final Object obj);  }### Answer:
@Test public void shouldGetSameHashCode_withDifferentObjects() { final String expectedKey = Randoms.randomString(20); final Direction expectedDirection = Randoms.randomEnum(Direction.class); final SortingOption sortingOption001 = new SortingOption(expectedKey, expectedDirection); final SortingOption sortingOption002 = new SortingOption(expectedKey, expectedDirection); assertEquals(sortingOption001.hashCode(), sortingOption002.hashCode()); }
@Test public void shouldGetHashCode_withNullKeyAndNullDirection() { final SortingOption sortingOption = new SortingOption(null, null); assertEquals(961, sortingOption.hashCode()); }
@Test public void shouldGetHashCode_withNullDirection() { final String expectedKey = Randoms.randomString(20); final SortingOption sortingOption = new SortingOption(expectedKey, null); assertTrue(String.format("%d should be different to the default: %d", sortingOption.hashCode(), 961), sortingOption.hashCode() != 961); }
@Test public void shouldGetHashCode_withNullKey() { final Direction expectedDirection = Randoms.randomEnum(Direction.class); final SortingOption sortingOption = new SortingOption(null, expectedDirection); assertTrue(String.format("%d should be different to the default: %d", sortingOption.hashCode(), 961), sortingOption.hashCode() != 961); }