Interface HashCodeContract<T>

Type Parameters:
T - The type being tested.
All Superinterfaces:
ContractSupport<T>, ProviderSupport<T>, SupportedMethods
All Known Subinterfaces:
CollectionContract<E,C>, IterableContract<E,I>, ListContract<E,L>, ObjectContract<T>, SequencedCollectionContract<E,C>, WithString

public interface HashCodeContract<T> extends ContractSupport<T>

This interface tests if a class has implemented the hashCode() method correctly. This contract class can be used individually by a test class, but it is normally used through the ObjectContract class:

public class MyClassTest extends ObjectContract<MyClass> {
}

If a test is using the ObjectContract class, but the class being tested does not implement the hashCode method based upon the specification in the Object class, then it can be omitted from the tests using the doesNotSupportMethod() method:

public class MyClassTest extends ObjectContract<MyClass> {
    public MyClassTest() {
        doesNotSupportMethod(ObjectMethods.HashCode);
    }
}
Since:
1.0
Author:
evanbergstrom
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    Test that the hashCode function results in a uniform distribution of hashcode values.
    default void
    Tests that the hashCode() method consistently returns the same integer value over multiple invocations.
    default void
    Tests that the hashCode() method returns the same integer value for objects that are equal.
    default void
    Tests that the hashCode() method returns different values for objects that are not equal.

    Methods inherited from interface org.soliscode.test.contract.support.ProviderSupport

    provider

    Methods inherited from interface org.soliscode.test.SupportedMethods

    supportsMethod
  • Method Details

    • testHashCodeIsStable

      @Test @DisplayName("hashCode() returns the same integer for multiple invocations.") default void testHashCodeIsStable()
      Tests that the hashCode() method consistently returns the same integer value over multiple invocations.
      Throws:
      org.opentest4j.AssertionFailedError - if the test fails.
      See Also:
    • testHashCodeWithEqualValues

      @Test @DisplayName("hashCode() returns the same integer for equals values") default void testHashCodeWithEqualValues()
      Tests that the hashCode() method returns the same integer value for objects that are equal.
      Throws:
      org.opentest4j.AssertionFailedError - if the test fails.
      See Also:
    • testHashDifferentValues

      @Test @DisplayName("hashCode() returns different integer for values that are not equal.") default void testHashDifferentValues()
      Tests that the hashCode() method returns different values for objects that are not equal. This is not strictly required by the Object interface, but it will result in improved performance.
      Throws:
      org.opentest4j.AssertionFailedError - if the test fails.
      See Also:
    • testHashCodeDistribution

      @Test @DisplayName("hashCode() returns integer that have a uniform distribution.") default void testHashCodeDistribution()
      Test that the hashCode function results in a uniform distribution of hashcode values.
      Throws:
      org.opentest4j.AssertionFailedError - if the test fails.
      See Also: