- Given the 4
@Testmethods shown, how many times does the@Beforemethod execute?
Just one, it initializes global variables. In this case, we are creating objects from the class we want to test. - The contract for
equals()states that no exceptions may be thrown. Instead,equals()is supposed to returnfalseif passed anullargument.
Write a JUnit test that verifies this property for theEHclass.@Test public void noNPE() { assertEquals(false, eh1.equals(null)); } - Using the given
EHobjects, write a test that verifies thatequals()returns false if the objects are, in fact, not equal.@Test public void equalsFalse() { assertEquals(false, eh1.equals(eh2)); } - Using the given
EHobjects, write a test that verifies thatequals()returns true if the objects are, in fact, equal.@Test public void equalsTrue() { assertEquals(true, eh1.equals(eh3)); } - Using the given EH objects, write a test to verify that hashCode() is consistent with equals. This test should fail if hashCode() is commented out (as shown), but pass if hashCode() is implemented.
@Test public void hashConsistent() { assertEquals(true, eh1.hashCode() == eh3.hashCode()); }