Package org.fest.test

Class EqualsHashCodeContractAssert

java.lang.Object
org.fest.test.EqualsHashCodeContractAssert

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

    Modifier and Type
    Method
    Description
    static void
    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
    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
    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
    Verifies that the equals implementation of the given object returns false when the object is compared to null.
    static void
    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 Details

    • assertIsNotEqualToNull

      public static void assertIsNotEqualToNull(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:
      AssertionError - if the equals implementation of the given objects returns true when the object compared to null.
      See Also:
    • assertEqualsIsReflexive

      public static void assertEqualsIsReflexive(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:
      AssertionError - if the equals implementation of the given object is reflexive.
    • assertEqualsIsSymmetric

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

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

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