### Verify Class using Examples for Prefab Values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Use `forExamples` to provide instances of a class that EqualsVerifier will use to infer prefab values. This method assumes all fields in the provided examples are different. ```java var red = new Book("Don Quixote", new Person("Cervantes", 1547)); var blue = new Book("Hitch-Hiker's Guide to the Galaxy", new Person("Douglas Adams", 1952)); // Let's assume both authors were married and their `marriedTo` field is correctly initialized EqualsVerifier.forExamples(red, blue) .verify(); ``` -------------------------------- ### Install Project in Local Maven Cache with Just Source: https://github.com/jqno/equalsverifier/blob/main/CONTRIBUTING.md Install the project into the local Maven repository. This is useful for testing integration with other projects. The Maven command includes a profile for releases. ```bash just local-install ``` ```bash mvn install -Prelease ``` -------------------------------- ### Bypass example requirement for cached hashCode Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/17-caching-hashcodes.md Use this approach when constructing an example instance is difficult, requiring the suppression of a specific warning. ```java EqualsVerifier.forClass(ObjectWithCachedHashCode.class) .withCachedHashCode("cachedHashCode", "calcHashCode", null) .suppress(Warning.NO_EXAMPLE_FOR_CACHED_HASHCODE) .verify(); ``` -------------------------------- ### Equals implementation with canEqual Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/coverage-is-not-100-percent.md Example of an equals method utilizing canEqual. ```java @Override public boolean equals(Object obj) { if (!(obj instanceof ColorPoint)) { return false; } ColorPoint other = (ColorPoint)obj; if (!other.canEqual(this)) { return false; } return super.equals(other) && color.equals(other.color); } ``` -------------------------------- ### Data Class Example Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/16-kotlin.md Example of a Kotlin data class. EqualsVerifier ignores properties not part of the primary constructor, like 'isEmpty' in this case. ```kotlin data class MyDataclass(val value: String) { val isEmpty = value.isEmpty() } ``` -------------------------------- ### canEqual implementation Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/coverage-is-not-100-percent.md Example of a canEqual method in a leaf node class. ```java @Override public boolean canEqual(Object obj) { return obj instanceof ColorPoint; } ``` -------------------------------- ### Analyze dependency tree output Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/unsupported-class-file-major-version.md Example output from mvn dependency:tree showing conflicting versions of transitive dependencies. ```text [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.4.3:test [INFO] | +- org.mockito:mockito-core:jar:3.6.28:test [INFO] | | \- net.bytebuddy:byte-buddy-agent:jar:1.10.20:test [INFO] [...] [INFO] +- nl.jqno.equalsverifier:equalsverifier:jar:3.12:test [INFO] | +- org.objenesis:objenesis:jar:3.3:test [INFO] | \- net.bytebuddy:byte-buddy:jar:1.10.20:test ``` -------------------------------- ### Example Error Message for Missing Factory Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/11-final-means-final.md The error message displayed when EqualsVerifier cannot instantiate a subclass without a provided factory. ```plaintext -> Cannot instantiate a subclass of NonConstructableSuper (attempted subclass: NonConstructableSubForNonConstructableSuper). Use the overload of #withRedefinedSubclass() to specify a subclass. ``` -------------------------------- ### Kotlin Delegation Example Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/16-kotlin.md Demonstrates Kotlin delegation with 'lazy' and 'object' delegation. EqualsVerifier requires 'kotlin-reflect' for proper handling and can map Kotlin names to bytecode names. ```kotlin data class StringContainer(s: String) class Foo(container: StringContainer) { private val bar: String by container::s // object delegation private val baz: String by lazy { ... } // lazy delegation } ``` -------------------------------- ### Verify Class with Mockito Integration Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Use this when Mockito is available on the classpath to automatically mock dependencies. This simplifies setup by reducing the need for explicit prefab values. ```java EqualsVerifier.forClass(Book.class) .verify(); ``` -------------------------------- ### Implement hashCode for Interval Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/significant-fields-equals-relies-on-foo-but-hashcode-does-not.md Example of a hashCode implementation that may cause collisions in EqualsVerifier due to XOR operations. ```java @Override public int hashCode() { return start.hashCode() ^ end.hashCode(); } ``` -------------------------------- ### Non-standard equality implementation Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/coverage-is-not-100-percent.md Example of equality logic that results in unreachable code paths. ```java @Override public boolean equals(Object obj) { if (!(obj instanceof Point)) { return false; } if (x == 42) { return false; } Point other = (Point)obj; return x == other.x && y == other.y; } ``` -------------------------------- ### Interface Delegation Example Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/16-kotlin.md Illustrates interface delegation in Kotlin. The delegate instance is stored in a field like '$$delegate_0', and direct translation to Kotlin properties can be unreliable. ```kotlin interface Foo { val foo: Int } data class FooImpl(override val foo: Int) : Foo class InterfaceDelegation(fooValue: Int) : Foo by FooImpl(fooValue) { override fun equals(other: Any?): (other is InterfaceDelegation) && foo == other.foo override fun hashCode(): Int = foo } ``` -------------------------------- ### Demonstrating broken symmetry in inheritance Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/09-inheritance.md Example showing how calling super.equals() in a subclass can lead to broken symmetry when using instanceof. ```java Point p = new Point(1, 1); ColorPoint cp = new ColorPoint(1, 1, RED); System.out.println(p.equals(cp)) // prints true System.out.println(cp.equals(p)) // prints false ``` -------------------------------- ### Person Class Definition Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Example of a simple class structure. ```java class Person { private final String name; private final int yearOfBirth; // Constructor, equals(), hashCode() omitted } ``` -------------------------------- ### Define a class with custom equality Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/12-relaxed-equality.md Example of a Rational class implementation where equality is determined by the result of division rather than individual field values. ```java class Rational { private final int numerator; private final int denominator; public equals(Object obj) { if (!(obj instanceof Rational)) { return false; } Rational other = (Rational)obj; return (numerator / denominator) == (other.numerator / other.denominator); // awfully bad implementation but you get the idea } // leaving out everything else for brevity } ``` -------------------------------- ### Implement Lombok Lazy Cached HashCode Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/significant-fields-equals-relies-on-foo-but-hashcode-does-not.md Example of a class using Lombok's @EqualsAndHashCode with LAZY cache strategy. ```java @RequiredArgsConstructor @EqualsAndHashCode(cacheStrategy = EqualsAndHashCode.CacheStrategy.LAZY) public class CachedHashCode { private final String foo; } ``` -------------------------------- ### Illustrating `getClass()` vs. Liskov substitution principle Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/05-instanceof-or-getclass.md This example demonstrates how `getClass()` can lead to inequality between a superclass and a subclass with identical fields, violating the Liskov substitution principle. ```java class Foo { private final int i; // ... } class SubFoo extends Foo {} Foo a = new Foo(); Foo b = new SubFoo(); System.out.println(a.equals(b)); ``` -------------------------------- ### Implement a cached hashCode class Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/17-caching-hashcodes.md Example of a Java class that calculates and stores its hashCode in a final field upon construction. ```java class ObjectWithCachedHashCode { private final String name; private final int cachedHashCode; public ObjectWithCachedHashCode(String name) { this.name = name; this.cachedHashCode = calcHashCode(); } @Override public final int hashCode() { return cachedHashCode; } private int calcHashCode() { return name.hashCode(); } // equals method elided for brevity } ``` -------------------------------- ### Example of an incorrect equals method Source: https://github.com/jqno/equalsverifier/blob/main/docs/_pages/faq.md This snippet demonstrates a flawed equals implementation that EqualsVerifier may fail to detect as incorrect. ```java public boolean equals(Object obj) { if (!(obj instanceof Synonym)) { return false; } Synonym other = (Synonym)obj; if ("equality".equals(word) && "sameness".equals(other.word)) { return true; } return word.equals(other.word); } ``` -------------------------------- ### Delegation with Triple-Equals Operator Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/16-kotlin.md Example showing limitations with delegates and the triple-equals operator. EqualsVerifier may not detect differences between '==' and '===' due to bytecode compilation. ```kotlin class Foo(container: StringContainer) { private val foo: String by container::s override fun equals(other: Any?): Boolean = (other is Foo) && foo === other.foo } ``` -------------------------------- ### Build Full Project with Just Source: https://github.com/jqno/equalsverifier/blob/main/CONTRIBUTING.md Use this command to build the entire project. Alternatively, use the Maven command. ```bash just verify ``` ```bash mvn clean verify ``` -------------------------------- ### Format Code with Just Source: https://github.com/jqno/equalsverifier/blob/main/CONTRIBUTING.md Apply code formatting rules to the project. This command utilizes Spotless via Maven. ```bash just format ``` ```bash mvn spotless:apply ``` -------------------------------- ### Run PITest Mutation Testing with Just Source: https://github.com/jqno/equalsverifier/blob/main/CONTRIBUTING.md Initiate mutation testing using PITest. The equivalent Maven command is also provided. ```bash just pitest ``` ```bash mvn clean test org.pitest:pitest-maven:mutationCoverage ``` -------------------------------- ### Cyclic Person Class Definition Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Example of a class with a recursive dependency. ```java class Person { private final String name; private final int yearOfBirth; private final Person marriedTo; // Constructor, equals(), hashCode() omitted } ``` -------------------------------- ### Run Tests with Just Source: https://github.com/jqno/equalsverifier/blob/main/CONTRIBUTING.md Execute all project tests using this command. Maven can also be used for this purpose. ```bash just test ``` ```bash mvn test ``` -------------------------------- ### Check dependency trees Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/unsupported-class-file-major-version.md Commands to inspect the dependency hierarchy in Maven and Gradle projects. ```bash mvn dependency:tree ``` ```bash gradle dependencies ``` -------------------------------- ### Lombok class definition Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/coverage-is-not-100-percent.md Example of a class using Lombok's @EqualsAndHashCode that generates null checks. ```java @EqualsAndHashCode public static final class Lombok { @Nonnull private final String s; public Lombok(String s) { this.s = s; } } ``` -------------------------------- ### Define test module-info.java with open module Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/14-jpms.md The recommended approach for tests is to open the module to allow reflection by EqualsVerifier and JUnit. ```java open module my.module { // Note: open exports my.module; // Same as before requires org.junit.jupiter.api; // For JUnit requires nl.jqno.equalsverifier; // For EqualsVerifier } ``` -------------------------------- ### Define main module-info.java Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/14-jpms.md The standard module definition for the main source code. ```java module my.module { exports my.module; } ``` -------------------------------- ### Configure Factory for Non-Instantiable Classes Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/11-final-means-final.md Provide a factory to instantiate a class and its subclass when automatic generation fails. ```java class Foo { private final int i; public Foo(int i, Object requireFactory) { this.i = i; } } EqualsVerifier.forClass(Foo.class) .withFactory( values -> new Foo(values.getInt("i"), new Object()) values -> new Foo(values.getInt("i"), new Object()) {} ) .verify(); ``` -------------------------------- ### Verify equals() and hashCode() with simple mode Source: https://github.com/jqno/equalsverifier/blob/main/docs/_pages/index.md Use this snippet for a simpler verification of the equals() and hashCode() contract, which can be more lenient. Ensure the class `Foo` is correctly defined. ```java import nl.jqno.equalsverifier.*; @Test public void simpleEqualsContract() { EqualsVerifier.simple().forClass(Foo.class).verify(); } ``` -------------------------------- ### Define test module-info.java with specific opens Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/14-jpms.md Use specific 'opens' directives to restrict reflection access to only necessary packages. ```java module my.module { exports my.module; opens my.module.package.model; // Open model package requires org.junit.jupiter.api; requires nl.jqno.equalsverifier; } ``` -------------------------------- ### Use a configuration object for multiple tests Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/08-several-classes-at-once.md Creates a shared configuration object to apply common settings across multiple class verifications. ```java ConfiguredEqualsVerifier ev = EqualsVerifier.configure().usingGetClass(); ev.forClass(Student.class) .withIgnoredFields("grade") .verify(); ev.forClass(Teacher.class) .withIgnoredFields("salary") .verify(); ev.forClass(Staff.class) .withRedefinedSubclass(Janitor.class) .verify(); ``` -------------------------------- ### Basic EqualsVerifier Usage Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/00-why-what-how.md Use this one-liner to verify your `equals` and `hashCode` methods. It ensures 100% coverage and checks all requirements of the `equals` contract. ```java EqualsVerifier.forClass(Foo.class).verify(); ``` -------------------------------- ### Configure EqualsVerifier with a factory method Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/cannot-instantiate-use-withfactory.md Use this approach when EqualsVerifier cannot instantiate a class via reflection. The factory lambda provides a way to construct instances manually. ```java EqualsVerifier.forClass(Foo.class) .withFactory(values -> new Foo(values.get("bar"))) .verify(); ``` -------------------------------- ### Customizing field-to-getter conversion in EqualsVerifier Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/10-jpa-entities.md Override the default JavaBeans convention for converting field names to getter names. This example handles fields prefixed with 'm_' and uses a custom getter naming strategy. ```java EqualsVerifier .forClass(Foo.class) .withFieldnameToGetterConverter( fn -> "get" + Character.toUpperCase(fn.charAt(2)) + fn.substring(3) ) .verify(); ``` -------------------------------- ### BigDecimal compareTo vs equals Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/bigdecimal-equality.md Demonstrates the difference between BigDecimal's compareTo() and equals() methods. Use this to understand why BigDecimal equality can be problematic. ```java BigDecimal one = new BigDecimal("1"); BigDecimal alsoOne = new BigDecimal("1.0"); // prints true - 1 is the same as 1.0 System.out.println(one.compareTo(alsoOne) == 0); // prints false - 1 is not the same as 1.0 System.out.println(one.equals(alsoOne)); ``` -------------------------------- ### Conceptual Instance Creation Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Conceptual representation of how EqualsVerifier compares instances. ```java var red = new Book(...); var blue = new Book(...); if (red.equals(blue)) { // is that what we expected? } ``` -------------------------------- ### Handle generic types with prefab values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/recursive-datastructure.md Demonstrates a scenario where standard prefab values fail for generic containers like SparseArray. ```java class SparseArrayContainer { private final SparseArray strings; private final SparseArray ints; // leaving out everything else for brevity } // ... EqualsVerifier.forClass(SparseArrayContainer.class) .withPrefabValues( SparseArray.class, new SparseArray(1), new SparseArray(2, 3)) .verify(); ``` -------------------------------- ### Providing Prefab Values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Using withPrefabValues to resolve cyclic dependencies. ```java var redPrefab = new Person(...); var bluePrefab = new Person(...); EqualsVerifier.forClass(Book.class) .withPrefabValues(Person.class, redPrefab, bluePrefab) .verify(); ``` -------------------------------- ### Configuring EqualsVerifier for `getClass()` Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/05-instanceof-or-getclass.md If your `equals` method uses `getClass()`, add `.usingGetClass()` to your EqualsVerifier configuration to avoid potential issues. ```java EqualsVerifier.forClass(Foo.class) .usingGetClass() .verify(); ``` -------------------------------- ### Basic Java Class Structure Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/02-good-equals.md A simple Java class with fields for name and age, intended for implementing equals and hashCode. ```java public class Person { private final String name; private final int age; public Point(String name, int age) { this.name = name; this.age = age; } // equals? // hashCode? } ``` -------------------------------- ### Configure Maven dependency management for cglib Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/noclassdeffounderror.md Add this to your pom.xml to ensure the correct version of cglib-nodep is used, preventing classpath conflicts. ```xml cglib cglib-nodep 2.2 ``` -------------------------------- ### Test classes in a package with exclusions Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/08-several-classes-at-once.md Verifies all classes in a package while excluding specific classes using ScanOption. ```java EqualsVerifier.forPackage( "com.example.app.domain", ScanOption.except(Helper.class)) .verify(); ``` -------------------------------- ### Skip Mockito and Provide Prefab Values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Instruct EqualsVerifier to skip Mockito integration and manually provide prefab values for specific types. This is useful if Mockito is not desired or causes conflicts. ```java EqualsVerifier.forClass(Book.class) .set(Mode.skipMockito()) .withPrefabValues(Person.class, ...) .verify(); ``` -------------------------------- ### Configure Factories for Complex Inheritance Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/11-final-means-final.md Use factory overloads with redefined superclass or subclass methods for complex inheritance hierarchies. ```java EqualsVerifier.forClass(Foo.class) .withFactory( values -> new Foo(values.getInt("i"), new Object()) values -> new Foo(values.getInt("i"), new Object()) {} ) .withRedefinedSubclass( values -> new SubFoo(values.getInt("i"), values.getString("s"), new Object()) ) .verify(); EqualsVerifier.forClass(SubFoo.class) .withFactory( values -> new SubFoo(values.getInt("i"), values.getString("s"), new Object()) values -> new SubFoo(values.getInt("i"), values.getString("s"), new Object()) {} ) .withRedefinedSuperclass( values -> new Foo(values.getInt("i"), new Object()) ) .verify(); ``` -------------------------------- ### Test a specific list of classes Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/08-several-classes-at-once.md Verifies a provided list of specific classes. ```java EqualsVerifier.forClasses(Student.class, Teacher.class, Staff.class, Address.class) .verify(); ``` -------------------------------- ### Use instanceof instead of getClass() Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/subclass-object-is-not-equal-to-an-instance-of-a-trivial-subclass-with-equal-fields.md When dealing with frameworks that create dynamic proxy subclasses, use `instanceof` in your equals method to ensure proper comparison. ```java public boolean equals(Object other) { if (!(other instanceof Foo)) { return false; } // ... } ``` -------------------------------- ### Verify equals contract with lenient settings Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/01-getting-started.md Uses the simple() method to suppress strict inheritance and non-final field warnings. ```java import nl.jqno.equalsverifier.*; @Test public void equalsContract() { EqualsVerifier.simple() .forClass(Foo.class) .verify(); } ``` -------------------------------- ### Custom object instantiation with withFactory Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/11-final-means-final.md Use `withFactory` when EqualsVerifier cannot instantiate an object using its default strategies. It accepts a lambda that takes a `Values` object and returns an instance of the class under test. ```java class Foo { private final int i; private final String s; private final Bar b; public Foo(int i, String s, Bar b, Object hereToMakeConstructorStrategyFail) { this.i = i; this.s = s; this.b = b; } } EqualsVerifier.forClass(Foo.class) .withFactory(values -> new Foo( values.getInt("i"), values.getString("s"), values.get("b"), new Object())) .verify(); ``` -------------------------------- ### Verify cached hashCode with factory Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/17-caching-hashcodes.md Use withFactory to handle cached hashCodes by ignoring the cached field during verification. ```java @Test public void testCachedHashCode() { EqualsVerifier.forClass(ObjectWithCachedHashCode.class) .withFactory(v -> new ObjectWithCachedHashCode(v.getString("name"))) .withIgnoredFields("cachedHashCode") .verify(); } ``` -------------------------------- ### Define a generic class for testing Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md A sample class containing generic fields that require prefab values for EqualsVerifier. ```java class Foo { private final Bar stringBar; private final Bar intBar; // Constructor, equals(), hashCode() omitted } ``` -------------------------------- ### Define a record with null check Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/record-failed-to-run-constructor.md This Java record 'Foo' has a constructor that throws NullPointerException if the input string 's' is null. Use this when null is not an acceptable value. ```java public record Foo(String s) { public Foo { Objects.requireNonNull(s); } } ``` -------------------------------- ### Provide prefab values for integer fields Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/record-failed-to-run-constructor.md Configure EqualsVerifier to use specific values (42 and 1337) for integer fields when testing. This is useful if the record's constructor rejects default or standard prefab values. ```java EqualsVerifier.forClass(Foo.class) .withPrefabValues(int.class, 42, 1337) .verify(); ``` -------------------------------- ### Define a record with a minimum value check Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/record-failed-to-run-constructor.md This Java record 'Foo' has a constructor that throws IllegalArgumentException if the integer 'i' is less than 42. Use this when a specific range or minimum value is required. ```java public record Foo(int i) { public Foo { if (i < 42) { throw new IllegalArgumentException(); } } } ``` -------------------------------- ### Test classes in a package Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/08-several-classes-at-once.md Verifies all classes within a specified package. ```java EqualsVerifier.forPackage("com.example.app.domain") .verify(); ``` -------------------------------- ### Implement AbstractEntity with final equals and hashCode Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/10-jpa-entities.md Define an abstract superclass with final equals and hashCode methods based on an ID field to simplify entity comparison. ```java @MappedSuperclass abstract class AbstractEntity { @Id @Column(...) private Long id; public Long getId() { return id; } @Override public final boolean equals(Object obj) { if (!(obj instanceof AbstractEntity)) { return false; } return Objects.equals(getId(), ((AbstractEntity)obj).getId()); } @Override public final int hashCode() { return Objects.hashCode(getId()); } } @Entity class Foo extends AbstractEntity { @Column(...) private String name; } ``` -------------------------------- ### Verify Kotlin Class Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/16-kotlin.md Use this to verify a Kotlin class with EqualsVerifier. Ensure you provide the Java-style reflection type. ```kotlin EqualsVerifier.forClass(Foo::class.java) .verify() ``` -------------------------------- ### Verify Class with Custom Integer Prefab Values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Provide custom prefab values for the `int` type when testing classes with invariants that conflict with default integer values. These values will be applied to all `int` fields. ```java EqualsVerifier.forClass(Adult.class) .withPrefabValues(int.class, 21, 99) .verify(); ``` -------------------------------- ### Combine suppressions and prefab values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/record-failed-to-run-constructor.md A combined approach to handle multiple constructor validation issues in records. Suppresses null and zero field warnings while providing custom prefab values for integers. ```java EqualsVerifier.forClass(Foo.class) .suppress(Warning.NULL_FIELDS, Warning.ZERO_FIELDS) .withPrefabValues(int.class, 42, 1337) .verify(); ``` -------------------------------- ### Configure EqualsVerifier with generic prefab values Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/15-prefab-values.md Use withGenericPrefabValues to define a factory for generic types, providing control over how instances are constructed. ```java EqualsVerifier.forClass(Foo.class) .withGenericPrefabValues(Bar.class, t -> new Bar<>(t)); ``` -------------------------------- ### Configure EqualsVerifier for Lombok Lazy HashCode Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/significant-fields-equals-relies-on-foo-but-hashcode-does-not.md Use withLombokCachedHashCode to test classes utilizing Lombok's lazy caching strategy. ```java EqualsVerifier.forClass(LazyPojo.class) .withLombokCachedHashCode(new CachedHashCode("bar")); ``` -------------------------------- ### Java 17 Equals Method with Pattern Matching Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/02-good-equals.md An implementation of the equals method using Java 17's pattern matching for instanceof. It handles null checks for non-primitive fields using Objects.equals. ```java public boolean equals(Object obj) { return obj instanceof Person other && Objects.equals(name, other.name) && age == other.age; } ``` -------------------------------- ### Java HashCode Method Source: https://github.com/jqno/equalsverifier/blob/main/docs/_manual/02-good-equals.md A recommended implementation for the hashCode method that should accompany an overridden equals method. It uses Objects.hash for convenience. ```java public int hashCode() { return Objects.hash(name, age); } ``` -------------------------------- ### Implement CachedHashCode class Source: https://github.com/jqno/equalsverifier/blob/main/docs/_errormessages/significant-fields-equals-relies-on-foo-but-hashcode-does-not.md A class structure demonstrating a manual cached hashCode implementation that requires special handling by EqualsVerifier. ```java class CachedHashCode { private final int foo; private final int bar; private int cachedHashCode = 0; public int hashCode() { if (cachedHashCode == 0) { cachedHashCode = calculateHashCode(); } return cachedHashCode; } private int calculateHashCode() { int result = 0; result += 31 * foo; result += 31 * bar; return result; } } ```