Class EqualsVerifier


  • public final class EqualsVerifier
    extends Object
    EqualsVerifier can be used in unit tests to verify whether the contract for the equals and hashCode methods in a class is met.

    The contracts are described in the Javadoc comments for Object.equals(Object) and Object.hashCode()

    To get started, use EqualsVerifier as follows:

    EqualsVerifier.forClass(My.class).verify();

    Or, if that's too strict:

    EqualsVerifier.simple().forClass(My.class).verify();

    For more information, see the documentation at http://www.jqno.nl/equalsverifier

    • Method Detail

      • configure

        public static ConfiguredEqualsVerifier configure()
        Creates a configuration object that can be reused with EqualsVerifier for multiple classes. It has a fluent API.

        Save the configuration in a variable of type EqualsVerifierApi and call forClass(Class) on it for each class whose equals and hashCode must be verified.

        Returns:
        A reusable configuration object with a fluent API.
      • simple

        public static ConfiguredEqualsVerifier simple()
        Creates a configuration object that is pre-configured so that it can be used with most IDE-generated equals and hashCode methods without any further configuration.
        Returns:
        A reusable configuration object with a fluent API.
      • forClass

        public static <T> SingleTypeEqualsVerifierApi<T> forClass​(Class<T> type)
        Factory method. For general use.
        Type Parameters:
        T - The type.
        Parameters:
        type - The class for which the equals method should be tested.
        Returns:
        A fluent API for EqualsVerifier.
      • forClasses

        public static MultipleTypeEqualsVerifierApi forClasses​(Iterable<Class<?>> classes)
        Factory method. For general use.
        Parameters:
        classes - An iterable containing the classes for which equals method should be tested.
        Returns:
        A fluent API for EqualsVerifier.
      • forClasses

        public static MultipleTypeEqualsVerifierApi forClasses​(Class<?> first,
                                                               Class<?> second,
                                                               Class<?>... more)
        Factory method. For general use.
        Parameters:
        first - A class for which the equals method should be tested.
        second - Another class for which the equals method should be tested.
        more - More classes for which the equals method should be tested.
        Returns:
        A fluent API for EqualsVerifier.
      • forPackage

        public static MultipleTypeEqualsVerifierApi forPackage​(String packageName)
        Factory method. For general use.

        Note that this operation may be slow. If the test is too slow, use forClasses(Class, Class, Class...) instead.

        Parameters:
        packageName - A package for which each class's equals should be tested.
        Returns:
        A fluent API for EqualsVerifier.
      • forPackage

        public static MultipleTypeEqualsVerifierApi forPackage​(String packageName,
                                                               boolean scanRecursively)
        Factory method. For general use.

        Note that this operation may be slow. If the test is too slow, use forClasses(Class, Class, Class...) instead.

        Parameters:
        packageName - A package for which each class's equals should be tested.
        scanRecursively - true to scan all sub-packages
        Returns:
        A fluent API for EqualsVerifier.
      • forRelaxedEqualExamples

        @SafeVarargs
        public static <T> RelaxedEqualsVerifierApi<T> forRelaxedEqualExamples​(T first,
                                                                              T second,
                                                                              T... more)
        Factory method. Asks for a list of equal, but not identical, instances of T.

        For use when T is a class which has relaxed equality rules. This happens when two instances of T are equal even though the its internal state is different.

        This could happen, for example, in a Rational class that doesn't normalize: new Rational(1, 2).equals(new Rational(2, 4)) would return true.

        Using this factory method requires that RelaxedEqualsVerifierApi.andUnequalExamples(Object, Object...) be called to supply a list of unequal instances of T.

        This method automatically suppresses Warning.ALL_FIELDS_SHOULD_BE_USED.

        Type Parameters:
        T - the type.
        Parameters:
        first - An instance of T.
        second - Another instance of T, which is equal, but not identical, to first.
        more - More instances of T, all of which are equal, but not identical, to one another and to first and second.
        Returns:
        A fluent API for a more relaxed EqualsVerifier.