Class EqualsVerifier
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 Summary
Modifier and TypeMethodDescriptionstatic ConfiguredEqualsVerifierCreates a configuration object that can be reused with EqualsVerifier for multiple classes.static <T> SingleTypeEqualsVerifierApi<T>Factory method.forClasses(Class<?> first, Class<?> second, Class<?>... more) Factory method.forClasses(Iterable<Class<?>> classes) Factory method.forPackage(String packageName) Factory method.forPackage(String packageName, boolean scanRecursively) Factory method.static <T> RelaxedEqualsVerifierApi<T>forRelaxedEqualExamples(T first, T second, T... more) Factory method.static ConfiguredEqualsVerifiersimple()Creates a configuration object that is pre-configured so that it can be used with most IDE-generatedequalsandhashCodemethods without any further configuration.
-
Method Details
-
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
EqualsVerifierApiand callforClass(Class)on it for each class whoseequalsandhashCodemust be verified.- Returns:
- A reusable configuration object with a fluent API.
-
simple
Creates a configuration object that is pre-configured so that it can be used with most IDE-generatedequalsandhashCodemethods without any further configuration.- Returns:
- A reusable configuration object with a fluent API.
-
forClass
Factory method. For general use.- Type Parameters:
T- The type.- Parameters:
type- The class for which theequalsmethod should be tested.- Returns:
- A fluent API for EqualsVerifier.
-
forClasses
Factory method. For general use.- Parameters:
classes- An iterable containing the classes for whichequalsmethod 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 theequalsmethod should be tested.second- Another class for which theequalsmethod should be tested.more- More classes for which theequalsmethod should be tested.- Returns:
- A fluent API for EqualsVerifier.
-
forPackage
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'sequalsshould be tested.- Returns:
- A fluent API for EqualsVerifier.
-
forPackage
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'sequalsshould 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, tofirst.more- More instances of T, all of which are equal, but not identical, to one another and tofirstandsecond.- Returns:
- A fluent API for a more relaxed EqualsVerifier.
-