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 ConfiguredEqualsVerifier
Creates 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.forPackage
(String packageName, Class<?> mustExtend) Factory method.static <T> RelaxedEqualsVerifierApi<T>
forRelaxedEqualExamples
(T first, T second, T... more) Factory method.static ConfiguredEqualsVerifier
simple()
Creates a configuration object that is pre-configured so that it can be used with most IDE-generatedequals
andhashCode
methods 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
EqualsVerifierApi
and callforClass(Class)
on it for each class whoseequals
andhashCode
must 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-generatedequals
andhashCode
methods 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 theequals
method should be tested.- Returns:
- A fluent API for EqualsVerifier.
-
forClasses
Factory method. For general use.- Parameters:
classes
- An iterable containing the classes for whichequals
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 theequals
method should be tested.second
- Another class for which theequals
method should be tested.more
- More classes for which theequals
method 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'sequals
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'sequals
should be tested.scanRecursively
- true to scan all sub-packages- 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.Also note that if
mustExtend
is given, and it exists withinpackageName
, it will NOT be included.- Parameters:
packageName
- A package for which each class'sequals
should be tested.mustExtend
- if not null, returns only classes that extend or implement this class.- 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 tofirst
andsecond
.- Returns:
- A fluent API for a more relaxed EqualsVerifier.
-