or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration-api.mdindex.mdmultiple-class-api.mdrelaxed-equality-api.mdsingle-class-api.mdwarning-system.md
tile.json

tessl/maven-nl-jqno-equalsverifier--equalsverifier

Java library for verifying the contract of equals and hashCode methods in unit tests

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/nl.jqno.equalsverifier/equalsverifier@3.17.x

To install, run

npx @tessl/cli install tessl/maven-nl-jqno-equalsverifier--equalsverifier@3.17.0

index.mddocs/

EqualsVerifier

EqualsVerifier 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.

Package Information

  • Package Name: nl.jqno.equalsverifier:equalsverifier
  • Package Type: maven
  • Language: Java
  • Installation: Add to Maven: <dependency><groupId>nl.jqno.equalsverifier</groupId><artifactId>equalsverifier</artifactId><version>3.17.5</version><scope>test</scope></dependency>

Core Imports

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

Basic Usage

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();
    }
}

Architecture

EqualsVerifier is built around several key patterns:

  • Fluent API: Method chaining interface for configuration and execution
  • Factory Pattern: Static factory methods for different verification scenarios
  • Builder Pattern: Incremental configuration of verification parameters
  • Strategy Pattern: Warning suppression system for different validation rules
  • Template Method: Base verification process with customizable steps

Capabilities

Single Class Verification

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();

Single Class API

Multiple Class Verification

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);

Multiple Class API

Configuration and Reuse

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
    );
}

Configuration API

Relaxed Equality Verification

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
    );
}

Relaxed Equality API

Warning Suppression System

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
}

Warning System

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);
}