Java library for verifying the contract of equals and hashCode methods in unit tests
npx @tessl/cli install tessl/maven-nl-jqno-equalsverifier--equalsverifier@3.17.0EqualsVerifier is a Java library that verifies whether the contract for the equals and hashCode methods in a class is met. It provides extensive testing capabilities including contract verification, null safety checking, symmetry and transitivity testing, and coverage of edge cases like inheritance hierarchies and complex field types.
<dependency><groupId>nl.jqno.equalsverifier</groupId><artifactId>equalsverifier</artifactId><version>3.17.5</version><scope>test</scope></dependency>import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;import nl.jqno.equalsverifier.EqualsVerifier;
public class PersonTest {
@Test
public void testEquals() {
EqualsVerifier.forClass(Person.class).verify();
}
@Test
public void testEqualsWithSimpleConfiguration() {
EqualsVerifier.simple()
.forClass(Person.class)
.verify();
}
}EqualsVerifier is built around several key patterns:
Core functionality for verifying equals and hashCode contracts for individual classes. Supports extensive configuration options for field handling, warning suppression, and inheritance scenarios.
public static <T> SingleTypeEqualsVerifierApi<T> forClass(Class<T> type);
public static ConfiguredEqualsVerifier simple();Batch verification capabilities for testing multiple classes with shared configuration. Supports package scanning, filtering, and exception handling for large codebases.
public static MultipleTypeEqualsVerifierApi forClasses(
Iterable<Class<?>> classes
);
public static MultipleTypeEqualsVerifierApi forClasses(
Class<?> first,
Class<?> second,
Class<?>... more
);
public static MultipleTypeEqualsVerifierApi forPackage(String packageName);Reusable configuration objects that can be applied across multiple verification scenarios. Enables consistent testing patterns and shared prefab values.
public static ConfiguredEqualsVerifier configure();
public final class ConfiguredEqualsVerifier implements EqualsVerifierApi<Void> {
public ConfiguredEqualsVerifier suppress(Warning... warnings);
public <S> ConfiguredEqualsVerifier withPrefabValues(
Class<S> otherType,
S red,
S blue
);
}Specialized verification for classes with relaxed equality rules where multiple instances can be equal despite different internal state. Common in normalized representations and value objects.
@SafeVarargs
public static <T> RelaxedEqualsVerifierApi<T> forRelaxedEqualExamples(
T first,
T second,
T... more
);
public final class RelaxedEqualsVerifierApi<T> {
public SingleTypeEqualsVerifierApi<T> andUnequalExample(T example);
@SafeVarargs
public final SingleTypeEqualsVerifierApi<T> andUnequalExamples(
T first,
T... more
);
}Comprehensive warning system for suppressing specific validation rules when they don't apply to particular use cases. Includes warnings for inheritance, field usage, JPA entities, and more.
public enum Warning {
STRICT_INHERITANCE,
NONFINAL_FIELDS,
NULL_FIELDS,
ALL_FIELDS_SHOULD_BE_USED,
REFERENCE_EQUALITY,
// ... 17 total warning types
}public final class EqualsVerifierReport {
public Class<?> getType();
public boolean isSuccessful();
public String getMessage();
public Throwable getCause();
public static EqualsVerifierReport success(Class<?> type);
public static EqualsVerifierReport failure(
Class<?> type,
String message,
Throwable cause
);
}
/**
* Functional interface for generating prefab values of some generic type T.
* For each generic type parameter for T, a value of that type will be supplied in the
* List parameter of apply(List).
*/
@FunctionalInterface
public interface Func<T> {
T apply(List<?> values);
}
/**
* Functional interface for generating prefab values of a generic type T that has
* exactly 1 generic parameter A.
* A value of A will be supplied in the supply(Object) method.
*/
@SuppressWarnings("unchecked")
@FunctionalInterface
public interface Func1<A, T> extends Func<T> {
@Override
default T apply(List<?> values) {
return supply((A) values.get(0));
}
T supply(A a);
}
/**
* Functional interface for generating prefab values of a generic type T that has
* exactly 2 generic parameters, A and B.
* Values of A and B will be supplied in the supply(Object, Object) method.
*/
@SuppressWarnings("unchecked")
@FunctionalInterface
public interface Func2<A, B, T> extends Func<T> {
@Override
default T apply(List<?> values) {
return supply((A) values.get(0), (B) values.get(1));
}
T supply(A a, B b);
}