Skip to content

Instantly share code, notes, and snippets.

@nhniches
Created March 14, 2013 19:20
Show Gist options
  • Save nhniches/5164333 to your computer and use it in GitHub Desktop.
Save nhniches/5164333 to your computer and use it in GitHub Desktop.
start of (not finished) tests for creating a valid equals and hashcode method
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public abstract class GenericEqualityTester {
// tests we need for all object equality
// o1 == o2, o1 != p1
// object type is the same or if different fails
// compare fields (equals) works as follows:
// reflexive ==> x.equals(x)
// symmetric ==> x.equals(y) and y.equals(x)
// transitive ==> x.equals(y) and y.equals(z) then x.equals(z)
// consistent ==> x.equals(y) is the same with multiple calls (no values
// changed)
// nullability ==> x.equals(null) for non-null must be false
private Object objectUnderTest;
// return a new object of type GenericEqualityTester<T>; factory method
/* protected static <V> GenericEqualityTester<V> factory() {
return new GenericEqualityTester<V>();
}
*/
protected abstract Object newObjectUnderTest();
// {return null;}
@Before
public void setUp() {
objectUnderTest = newObjectUnderTest();
}
@Test
public void testObjectIdentityIsTrue() {
assertSame("object identity (o==o)", objectUnderTest, objectUnderTest);
}
@Test
public void testObjectIdentityIsTrue2() {
assertTrue("object identity (o==o)", (objectUnderTest == newObjectUnderTest()));
}
@Test
public void testObjectIdentityIsDifferentTypeFails() {
assertFalse("object identity (o!=p)", objectUnderTest == new MyClass());
}
@Test
public void testObjectEqualityIsDifferentTypeFails() {
assertFalse("object identity (o!=p)", objectUnderTest.equals(new MyClass()));
}
@Test
public void testObjectEqualityIsReflexive() {
assertTrue("object equality (x.equals(x))", objectUnderTest.equals(objectUnderTest));
}
@Test
public void testObjectEqualityIsSymetric() {
Object anotherObject = newObjectUnderTest();
boolean results = objectUnderTest.equals(anotherObject) && anotherObject.equals(objectUnderTest);
assertTrue("object equality (x.equals(y) and y.equals(x))", results);
}
@Test
public void testObjectEqualityIsConsistent() {
Object anotherObject = newObjectUnderTest();
boolean results;
// multiple calls to the same objects should return the same results
boolean results1 = objectUnderTest.equals(anotherObject);
boolean results2 = objectUnderTest.equals(anotherObject);
boolean results3 = objectUnderTest.equals(anotherObject);
boolean results4 = objectUnderTest.equals(anotherObject);
results = results1 && results2 && results3 && results4;
assertTrue("object equality (x.equals(y) is the same with multiple calls)", results);
}
@Test
public void testObjectEqualityIsTransitive() {
Object secondObject = newObjectUnderTest();
Object thirdObject = newObjectUnderTest();
boolean results = objectUnderTest.equals(secondObject) && secondObject.equals(thirdObject)
&& objectUnderTest.equals(thirdObject);
assertTrue("object equality (x.equals(y) and y.equals(z) then x.equals(z))", results);
}
@Test
public void testObjectEqualityIsNullablity() {
assertFalse("object equality x.equals(null) for non-null must be false)", objectUnderTest.equals(null));
}
class MyClass {
} // simple type
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment