Package org.fest.test
Class EqualsHashCodeContractAssert
java.lang.Object
org.fest.test.EqualsHashCodeContractAssert
Assertion methods that verify that an object's
equals
and hashCode
are implemented correctly.- Author:
- Alex Ruiz
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Verifies that theequals
implementation of the given object is reflexive: the object must be equal to itself, which it would be at any given instance; unless you intentionally override the equals method to behave otherwise.static void
assertEqualsIsSymmetric
(Object obj1, Object obj2) Verifies that theequals
implementation of the given objects is symmetric: if object of one class is equal to another class object, the other class object must be equal to this class object.static void
assertEqualsIsTransitive
(Object obj1, Object obj2, Object obj3) Verifies that theequals
implementation of the given objects is transitive: if the first object is equal to the second object and the second object is equal to the third object; then the first object is equal to the third object.static void
Verifies that theequals
implementation of the given object returnsfalse
when the object is compared tonull
.static void
assertMaintainsEqualsAndHashCodeContract
(Object obj1, Object obj2) Verifies that theequals
/hashCode
contract of the given objects is implemented correctly: if two objects are equal, then they must have the same hash code, however the opposite is NOT true.
-
Method Details
-
assertIsNotEqualToNull
Verifies that theequals
implementation of the given object returnsfalse
when the object is compared tonull
.- Parameters:
obj
- the object to verify.- Throws:
AssertionError
- if theequals
implementation of the given objects returnstrue
when the object compared tonull
.- See Also:
-
assertEqualsIsReflexive
Verifies that theequals
implementation of the given object is reflexive: the object must be equal to itself, which it would be at any given instance; unless you intentionally override the equals method to behave otherwise.- Parameters:
obj
- the object to verify.- Throws:
AssertionError
- if theequals
implementation of the given object is reflexive.
-
assertEqualsIsSymmetric
Verifies that theequals
implementation of the given objects is symmetric: if object of one class is equal to another class object, the other class object must be equal to this class object. In other words, one object can not unilaterally decide whether it is equal to another object; two objects, and consequently the classes to which they belong, must bilaterally decide if they are equal or not. They BOTH must agree.- Parameters:
obj1
- the object to verify.obj2
- the object to compare to.- Throws:
AssertionError
- if theequals
implementation of the given object is not symmetric.
-
assertEqualsIsTransitive
Verifies that theequals
implementation of the given objects is transitive: if the first object is equal to the second object and the second object is equal to the third object; then the first object is equal to the third object. In other words, if two objects agree that they are equal, and follow the symmetry principle, one of them can not decide to have a similar contract with another object of different class. All three must agree and follow symmetry principle for various permutations of these three classes.- Parameters:
obj1
- the object to verify.obj2
- an object to compare to.obj3
- an object to compare to.- Throws:
AssertionError
- if theequals
implementation of the given objects is not transitive.
-
assertMaintainsEqualsAndHashCodeContract
Verifies that theequals
/hashCode
contract of the given objects is implemented correctly: if two objects are equal, then they must have the same hash code, however the opposite is NOT true.- Parameters:
obj1
- the object to verify.obj2
- the object to compare to.- Throws:
AssertionError
- if theequals
/hashCode
contract of the given objects is not implemented correctly.
-