Class SingleTypeEqualsVerifierApi<T>
- Type Parameters:
T- The class under test.
- All Implemented Interfaces:
EqualsVerifierApi<T>
EqualsVerifier test with a fluent API.- Since:
- 3.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate CachedHashCodeInitializer<T> private FieldCacheprivate booleanprivate InstanceFactory<T> private final org.objenesis.Objenesisprivate InstanceFactory<? extends T> private InstanceFactory<?> private InstanceFactory<? extends T> private UserPrefabValueCachesprivate boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionSingleTypeEqualsVerifierApi(Class<T> type) Constructor.(package private)SingleTypeEqualsVerifierApi(Class<T> type, EnumSet<Warning> warningsToSuppress, Set<Mode> modes, UserPrefabValueCaches userPrefabs, org.objenesis.Objenesis objenesis, boolean usingGetClass, Function<String, String> converter) Constructor.(package private)Constructor, only to be called byRelaxedEqualsVerifierApi.andUnequalExamples(Object, Object[]).privateSingleTypeEqualsVerifierApi(Class<T> type, org.objenesis.Objenesis objenesis) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate Configuration<T> private StringbuildErrorMessage(String description, boolean showUrl) private voidreport()Performs the verification of the contracts forequalsandhashCodeand returns anEqualsVerifierReportwith the results of the verification.report(boolean showUrl) Performs the verification of the contracts forequalsandhashCodeand returns anEqualsVerifierReportwith the results of the verification.Sets modes that influence howEqualsVerifieroperates.Suppresses warnings given byEqualsVerifier.Signals thatgetClassis used in the implementation of theequalsmethod, instead of aninstanceofcheck.voidverify()Performs the verification of the contracts forequalsandhashCodeand throws anAssertionErrorif there is a problem.private voidverifyWithExamples(Context<T> context) private voidverifyWithoutExamples(Context<T> context) withCachedHashCode(String cachedHashCodeField, String calculateHashCodeMethod, T example) Signals that T caches its hashCode, instead of re-calculating it each time thehashCode()method is called.withFactory(InstanceFactory<T> factory) Provides a factory for the class under test, in case EqualsVerifier is unable to instantiate it without help.withFactory(InstanceFactory<T> factory, InstanceFactory<? extends T> subclassFactory) Provides a factory for the class under test, in case EqualsVerifier is unable to instantiate it without help, as well as a factory for a subclass of the class under test.withFieldnameToGetterConverter(Function<String, String> converter) Determines how a getter name can be derived from a field name.private SingleTypeEqualsVerifierApi<T> withFieldsAddedAndValidated(Set<String> collection, List<String> specifiedFields) withGenericPrefabValues(Class<S> otherType, Func.Func1<?, S> factory) Adds a factory to generate prefabricated values for instance fields of classes with 1 generic type parameter that EqualsVerifier cannot instantiate by itself.withGenericPrefabValues(Class<S> otherType, Func.Func2<?, ?, S> factory) Adds a factory to generate prefabricated values for instance fields of classes with 2 generic type parameters that EqualsVerifier cannot instantiate by itself.withIgnoredAnnotations(Class<?>... annotations) Signals that all given annotations are to be ignored by EqualsVerifier.withIgnoredFields(String... fields) Signals that all given fields are not relevant for theequalscontract.withLombokCachedHashCode(T example) Signals that T uses Lombok to cache its hashCode, instead of re-calculating it each time thehashCode()method is called.withNonnullFields(String... fields) Signals that all given fields can never be null, andEqualsVerifiertherefore doesn't have to verify that proper null checks are in place for these fields.withOnlyTheseFields(String... fields) Signals that all given fields, and only the given fields, are relevant for theequalscontract.withPrefabValues(Class<S> otherType, S red, S blue) Adds prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself.withPrefabValues(Class<S> otherType, S red, S blue, S redCopy) Adds prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself.withPrefabValuesForField(String fieldName, S red, S blue) Adds prefabricated values for instance fields with a given name (and only the fields with the given name) that EqualsVerifier cannot instantiate by itself.withPrefabValuesForField(String fieldName, S red, S blue, S redCopy) Adds prefabricated values for instance fields with a given name (and only the fields with the given name) that EqualsVerifier cannot instantiate by itself.withRedefinedSubclass(Class<? extends T> subclass) Supplies a reference to a subclass of T in whichequalsis overridden.withRedefinedSubclass(InstanceFactory<? extends T> subclassFactory) Supplies a factory for a subclass of T in whichequalsis overridden.Signals that T is part of an inheritance hierarchy whereequalsis overridden.withRedefinedSuperclass(InstanceFactory<?> superclassFactory) Signals that T is part of an inheritance hierarchy whereequalsis overridden.withResettablePrefabValues(Class<S> otherType, Supplier<S> red, Supplier<S> blue) Adds Suppliers for resettable prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself.
-
Field Details
-
type
-
actualFields
-
instanceFactory
-
subclassInstanceFactory
-
warningsToSuppress
-
modesToSet
-
usingGetClass
private boolean usingGetClass -
hasRedefinedSuperclass
private boolean hasRedefinedSuperclass -
redefinedSuperclassInstanceFactory
-
redefinedSubclass
-
redefinedSubclassInstanceFactory
-
userPrefabs
-
fieldCache
-
cachedHashCodeInitializer
-
fieldnameToGetter
-
allExcludedFields
-
allIncludedFields
-
nonnullFields
-
ignoredAnnotationClassNames
-
equalExamples
-
unequalExamples
-
objenesis
private final org.objenesis.Objenesis objenesis
-
-
Constructor Details
-
SingleTypeEqualsVerifierApi
Constructor.- Parameters:
type- The class for which theequalsmethod should be tested.- Since:
- 3.2
-
SingleTypeEqualsVerifierApi
Constructor.- Parameters:
type- The class for which theequalsmethod should be tested.objenesis- To instantiate non-record classes.
-
SingleTypeEqualsVerifierApi
SingleTypeEqualsVerifierApi(Class<T> type, EnumSet<Warning> warningsToSuppress, Set<Mode> modes, UserPrefabValueCaches userPrefabs, org.objenesis.Objenesis objenesis, boolean usingGetClass, Function<String, String> converter) Constructor.- Parameters:
type- The class for which theequalsmethod should be tested.warningsToSuppress- A list of warnings to suppress inEqualsVerifier.modes- A set of modes in whichEqualsVerifiershould operate.userPrefabs- Prefab values provided by the user.objenesis- To instantiate non-record classes.usingGetClass- WhethergetClassis used in the implementation of theequalsmethod, instead of aninstanceofcheck.converter- A function that converts from field name to getter name.
-
SingleTypeEqualsVerifierApi
Constructor, only to be called byRelaxedEqualsVerifierApi.andUnequalExamples(Object, Object[]).
-
-
Method Details
-
withFactory
Provides a factory for the class under test, in case EqualsVerifier is unable to instantiate it without help. This becomes useful in Java 26 where JEP 500 ("final means final") is active.- Parameters:
factory- A factory that can instantiate the class under test.- Returns:
this, for easy method chaining.- Since:
- 4.4
-
withFactory
public SingleTypeEqualsVerifierApi<T> withFactory(InstanceFactory<T> factory, InstanceFactory<? extends T> subclassFactory) Provides a factory for the class under test, in case EqualsVerifier is unable to instantiate it without help, as well as a factory for a subclass of the class under test. This is needed when the class under test is non-final, so EqualsVerifier can check the interaction with subclasses. This becomes useful in Java 26 where JEP 500 ("final means final") is active.- Parameters:
factory- A factory that can instantiate the class under test.subclassFactory- A factory that can instantiate a subclass of the class under test.- Returns:
this, for easy method chaining.- Since:
- 4.4
-
suppress
Suppresses warnings given byEqualsVerifier. SeeWarningto see what warnings can be suppressed.- Specified by:
suppressin interfaceEqualsVerifierApi<T>- Parameters:
warnings- A list of warnings to suppress inEqualsVerifier.- Returns:
this, for easy method chaining.
-
set
Sets modes that influence howEqualsVerifieroperates.}- Specified by:
setin interfaceEqualsVerifierApi<T>- Parameters:
modes- A list of modes to influence howEqualsVerifieroperates.- Returns:
this, for easy method chaining.
-
withPrefabValues
Adds prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself. This overload will attempt to copyred. If that fails, use the other overload.- Specified by:
withPrefabValuesin interfaceEqualsVerifierApi<T>- Type Parameters:
S- The class of the prefabricated values.- Parameters:
otherType- The class of the prefabricated values.red- An instance ofS.blue- Another instance ofS, not equal tored.- Returns:
this, for easy method chaining.
-
withPrefabValues
public <S> SingleTypeEqualsVerifierApi<T> withPrefabValues(Class<S> otherType, S red, S blue, S redCopy) Adds prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself.- Specified by:
withPrefabValuesin interfaceEqualsVerifierApi<T>- Type Parameters:
S- The class of the prefabricated values.- Parameters:
otherType- The class of the prefabricated values.red- An instance ofS.blue- Another instance ofS, not equal tored.redCopy- A copy ofred.- Returns:
this, for easy method chaining.
-
withResettablePrefabValues
public <S> SingleTypeEqualsVerifierApi<T> withResettablePrefabValues(Class<S> otherType, Supplier<S> red, Supplier<S> blue) Adds Suppliers for resettable prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself. Resettable values are values that are created new every time a new instance ofTis constructed within EqualsVerifier. This can be useful whenTmutates its instances ofS.- Specified by:
withResettablePrefabValuesin interfaceEqualsVerifierApi<T>- Type Parameters:
S- The class of the prefabricated values.- Parameters:
otherType- The class of the prefabricated values.red- An instance ofS.blue- Another instance ofS, not equal tored.- Returns:
this, for easy method chaining.
-
withPrefabValuesForField
Adds prefabricated values for instance fields with a given name (and only the fields with the given name) that EqualsVerifier cannot instantiate by itself. This overload will attempt to copyred. If that fails, use the other overload.- Type Parameters:
S- The class of the prefabricated values.- Parameters:
fieldName- The name of the field that the prefabricated values are linked to.red- An instance ofS.blue- Another instance ofS, not equal tored.- Returns:
this, for easy method chaining.- Throws:
NullPointerException- Ifredorblueis null, or if the named field does not exist in the class.IllegalArgumentException- Ifredequalsblue.- Since:
- 3.17
-
withPrefabValuesForField
public <S> SingleTypeEqualsVerifierApi<T> withPrefabValuesForField(String fieldName, S red, S blue, S redCopy) Adds prefabricated values for instance fields with a given name (and only the fields with the given name) that EqualsVerifier cannot instantiate by itself.- Type Parameters:
S- The class of the prefabricated values.- Parameters:
fieldName- The name of the field that the prefabricated values are linked to.red- An instance ofS.blue- Another instance ofS, not equal tored.redCopy- A copy ofred.- Returns:
this, for easy method chaining.- Throws:
NullPointerException- Ifredorblueis null, or if the named field does not exist in the class.IllegalArgumentException- Ifredequalsblue.- Since:
- 4.4
-
withGenericPrefabValues
public <S> SingleTypeEqualsVerifierApi<T> withGenericPrefabValues(Class<S> otherType, Func.Func1<?, S> factory) Adds a factory to generate prefabricated values for instance fields of classes with 1 generic type parameter that EqualsVerifier cannot instantiate by itself.- Specified by:
withGenericPrefabValuesin interfaceEqualsVerifierApi<T>- Type Parameters:
S- The class of the prefabricated values.- Parameters:
otherType- The class of the prefabricated values.factory- A factory to generate an instance ofS, given a value of its generic type parameter.- Returns:
this, for easy method chaining.
-
withGenericPrefabValues
public <S> SingleTypeEqualsVerifierApi<T> withGenericPrefabValues(Class<S> otherType, Func.Func2<?, ?, S> factory) Adds a factory to generate prefabricated values for instance fields of classes with 2 generic type parameters that EqualsVerifier cannot instantiate by itself.- Specified by:
withGenericPrefabValuesin interfaceEqualsVerifierApi<T>- Type Parameters:
S- The class of the prefabricated values.- Parameters:
otherType- The class of the prefabricated values.factory- A factory to generate an instance ofS, given a value of each of its generic type parameters.- Returns:
this, for easy method chaining.
-
usingGetClass
Signals thatgetClassis used in the implementation of theequalsmethod, instead of aninstanceofcheck.- Specified by:
usingGetClassin interfaceEqualsVerifierApi<T>- Returns:
this, for easy method chaining.- See Also:
-
withFieldnameToGetterConverter
public SingleTypeEqualsVerifierApi<T> withFieldnameToGetterConverter(Function<String, String> converter) Determines how a getter name can be derived from a field name. The default behavior is to uppercase the field's first letter and prepend 'get'. For instance, a field name 'employee' would correspond to getter name 'getEmployee'. This method can be used if your project has a different naming convention.- Specified by:
withFieldnameToGetterConverterin interfaceEqualsVerifierApi<T>- Parameters:
converter- A function that converts from field name to getter name.- Returns:
this, for easy method chaining.
-
withIgnoredFields
Signals that all given fields are not relevant for theequalscontract.EqualsVerifierwill not fail if one of these fields does not affect the outcome ofequals, but it will fail if one of these fields does affect the outcome ofequals.Note that these fields will still be used to test for null-ness, among other things. Therefore, EqualsVerifier needs values for these fields, and will ask for prefab values if it can't make them.
- Parameters:
fields- Fields that should be ignored.- Returns:
this, for easy method chaining.- Since:
- 2.0
-
withOnlyTheseFields
Signals that all given fields, and only the given fields, are relevant for theequalscontract.EqualsVerifierwill fail if one of these fields does not affect the outcome ofequals, and it will fail if a field that isn't listed here, does affect the outcome ofequals.- Parameters:
fields- Fields that should be ignored.- Returns:
this, for easy method chaining.- Since:
- 2.0
-
withFieldsAddedAndValidated
-
withNonnullFields
Signals that all given fields can never be null, andEqualsVerifiertherefore doesn't have to verify that proper null checks are in place for these fields.This can be used instead of {link #suppress(Warning...)}, which provides the same behaviour for all fields, when only some fields are never null but others are.
- Parameters:
fields- Fields that can never be null.- Returns:
this, for easy method chaining.- Since:
- 2.2
-
withIgnoredAnnotations
Signals that all given annotations are to be ignored by EqualsVerifier.For instance, EqualsVerifier normally doesn't perform null verifications on fields marked with an
@Nonnullannotation. However, if this method is called with aNonnull.classparameter, the null verifications will be performed after all.- Parameters:
annotations- Annotations to ignore.- Returns:
this, for easy method chaining.- Since:
- 2.3
-
withRedefinedSuperclass
Signals that T is part of an inheritance hierarchy whereequalsis overridden. Call this method if T has overriddenequalsandhashCode, and one or more of T's superclasses have as well.T itself does not necessarily have to have subclasses that redefine
equalsandhashCode.- Returns:
this, for easy method chaining.- Since:
- 0.6
-
withRedefinedSuperclass
Signals that T is part of an inheritance hierarchy whereequalsis overridden. Call this method if T has overriddenequalsandhashCode, and one or more of T's superclasses have as well.Provides a factory for the superclass, in case EqualsVerifier is unable to instantiate it without help. This becomes useful in Java 26 where JEP 500 ("final means final") is active.
T itself does not necessarily have to have subclasses that redefine
equalsandhashCode.- Parameters:
superclassFactory- A factory that can instantiate the direct superclass of the class under test.- Returns:
this, for easy method chaining.- Since:
- 4.4
-
withRedefinedSubclass
Supplies a reference to a subclass of T in whichequalsis overridden. Calling this method is mandatory ifequalsis not final and a strong verification is performed.Note that, for each subclass that overrides
equals,EqualsVerifiershould be used as well to verify its adherence to the contracts.- Parameters:
subclass- A subclass of T for which no instance can be equal to any instance of T.- Returns:
this, for easy method chaining.- Since:
- 0.1
- See Also:
-
withRedefinedSubclass
public SingleTypeEqualsVerifierApi<T> withRedefinedSubclass(InstanceFactory<? extends T> subclassFactory) Supplies a factory for a subclass of T in whichequalsis overridden. Calling this method is mandatory ifequalsis not final and a strong verification is performed.This becomes useful in Java 26 where JEP 500 ("final means final") is active.
Note that, for each subclass that overrides
equals,EqualsVerifiershould be used as well to verify its adherence to the contracts.- Parameters:
subclassFactory- A factory that can instantiate a subclass of T for which no instance can be equal to any instance of T.- Returns:
this, for easy method chaining.- Since:
- 4.4
- See Also:
-
withCachedHashCode
public SingleTypeEqualsVerifierApi<T> withCachedHashCode(String cachedHashCodeField, String calculateHashCodeMethod, T example) Signals that T caches its hashCode, instead of re-calculating it each time thehashCode()method is called.There are 3 conditions to verify cached hashCodes:
First, the class under test must have a private int field that contains the cached hashCode.
Second, the class under test must have a private method that calculates the hashCode. The method must return an int and may not take any parameters. It should be used by the constructor or the hashCode method of the class under test to initialize the cached hashCode. This may lead to slightly awkward production code, but unfortunately, it is necessary for EqualsVerifier to verify that the hashCode is correct.
Finally, only immutable objects can be verified. In other words,
withCachedHashCodecan not be used whenWarning.NONFINAL_FIELDSis suppressed.- Parameters:
cachedHashCodeField- The name of the field which stores the cached hash code.calculateHashCodeMethod- The name of the method which recomputes the hash code. It should return an int and take no parameters.example- An instance of the class under test, to verify that the hashCode has been initialized properly.- Returns:
this, for easy method chaining.- Since:
- 1.7
-
withLombokCachedHashCode
Signals that T uses Lombok to cache its hashCode, instead of re-calculating it each time thehashCode()method is called.- Parameters:
example- An instance of the class under test, to verify that the hashCode has been initialized properly.- Returns:
this, for easy method chaining.- Since:
- 3.7
- See Also:
-
verify
public void verify()Performs the verification of the contracts forequalsandhashCodeand throws anAssertionErrorif there is a problem.- Throws:
AssertionError- If the contract is not met, or ifEqualsVerifier's preconditions do not hold.- Since:
- 0.1
-
report
Performs the verification of the contracts forequalsandhashCodeand returns anEqualsVerifierReportwith the results of the verification.- Returns:
- An
EqualsVerifierReportthat indicates whether the contract is met and whetherEqualsVerifier's preconditions hold. - Since:
- 3.0
-
report
Performs the verification of the contracts forequalsandhashCodeand returns anEqualsVerifierReportwith the results of the verification.- Parameters:
showUrl- Whether or not to show the url at the end of the error message.- Returns:
- An
EqualsVerifierReportthat indicates whether the contract is met and whetherEqualsVerifier's preconditions hold. - Since:
- 3.2
-
buildErrorMessage
-
performVerification
private void performVerification() -
buildConfig
-
verifyWithoutExamples
-
verifyWithExamples
-