I created an extension to the EqualsHashCodeTestCase that uses reflections to test equality for all attributes in a POJO. It's used in exactly the same way as EqualsHashCodeTestCase.
This is what it does:
1) assert equalObj1.equals(equalObj2) and !equalObj1.equals(differentObj)
2) for all attributes in the class not annotated by ExcludeFromEquals
3a) change the attributes' value the value in equalObj1
3b) assert !equalObj1.equals(equalObj2) && equalObj1.hashCode() != equalObj2.hashCode()
3c) restore original value to the attribute in equalObj1.
I use it at work for most POJOS. If a developer adds/removes attributes and forgets to update the equality and hashcode methods our test suite will fail.
Attached tarball with the testsuite, annotation and a test case.
Depends on JRE 1.5, junit-3.8.1 and junit-addons-1.4
Log in to post a comment.