Class EqualsVerifier
- java.lang.Object
-
- nl.jqno.equalsverifier.EqualsVerifier
-
public final class EqualsVerifier extends Object
EqualsVerifier
can be used in unit tests to verify whether the contract for theequals
andhashCode
methods in a class is met.The contracts are described in the Javadoc comments for
Object.equals(Object)
andObject.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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ConfiguredEqualsVerifier
configure()
Creates a configuration object that can be reused with EqualsVerifier for multiple classes.static <T> SingleTypeEqualsVerifierApi<T>
forClass(Class<T> type)
Factory method.static MultipleTypeEqualsVerifierApi
forClasses(Class<?> first, Class<?> second, Class<?>... more)
Factory method.static MultipleTypeEqualsVerifierApi
forClasses(Iterable<Class<?>> classes)
Factory method.static MultipleTypeEqualsVerifierApi
forPackage(String packageName)
Factory method.static MultipleTypeEqualsVerifierApi
forPackage(String packageName, boolean scanRecursively)
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 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 callforClass(Class)
on it for each class whoseequals
andhashCode
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-generatedequals
andhashCode
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 theequals
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 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
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'sequals
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'sequals
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, 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.
-
-