Package org.fest.test

Class EqualsHashCodeContractAssert


  • public final class EqualsHashCodeContractAssert
    extends java.lang.Object
    Assertion methods that verify that an object's equals and hashCode are implemented correctly.
    Author:
    Alex Ruiz
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertEqualsIsReflexive​(java.lang.Object obj)
      Verifies that the equals 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​(java.lang.Object obj1, java.lang.Object obj2)
      Verifies that the equals 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​(java.lang.Object obj1, java.lang.Object obj2, java.lang.Object obj3)
      Verifies that the equals 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 assertIsNotEqualToNull​(java.lang.Object obj)
      Verifies that the equals implementation of the given object returns false when the object is compared to null.
      static void assertMaintainsEqualsAndHashCodeContract​(java.lang.Object obj1, java.lang.Object obj2)
      Verifies that the equals/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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • assertIsNotEqualToNull

        public static void assertIsNotEqualToNull​(java.lang.Object obj)
        Verifies that the equals implementation of the given object returns false when the object is compared to null.
        Parameters:
        obj - the object to verify.
        Throws:
        java.lang.AssertionError - if the equals implementation of the given objects returns true when the object compared to null.
        See Also:
        EqualsHashCodeContractTestCase.should_not_be_equal_to_null()
      • assertEqualsIsReflexive

        public static void assertEqualsIsReflexive​(java.lang.Object obj)
        Verifies that the equals 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:
        java.lang.AssertionError - if the equals implementation of the given object is reflexive.
      • assertEqualsIsSymmetric

        public static void assertEqualsIsSymmetric​(java.lang.Object obj1,
                                                   java.lang.Object obj2)
        Verifies that the equals 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:
        java.lang.AssertionError - if the equals implementation of the given object is not symmetric.
      • assertEqualsIsTransitive

        public static void assertEqualsIsTransitive​(java.lang.Object obj1,
                                                    java.lang.Object obj2,
                                                    java.lang.Object obj3)
        Verifies that the equals 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:
        java.lang.AssertionError - if the equals implementation of the given objects is not transitive.
      • assertMaintainsEqualsAndHashCodeContract

        public static void assertMaintainsEqualsAndHashCodeContract​(java.lang.Object obj1,
                                                                    java.lang.Object obj2)
        Verifies that the equals/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:
        java.lang.AssertionError - if the equals/hashCode contract of the given objects is not implemented correctly.