Class SingleTypeEqualsVerifierApi<T>
- Type Parameters:
T
- The class under test.
- All Implemented Interfaces:
EqualsVerifierApi<T>
EqualsVerifier
test with a fluent API.-
Constructor Summary
ConstructorDescriptionSingleTypeEqualsVerifierApi
(Class<T> type) Constructor.SingleTypeEqualsVerifierApi
(Class<T> type, EnumSet<Warning> warningsToSuppress, FactoryCache factoryCache, boolean usingGetClass, Function<String, String> converter) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionreport()
Performs the verification of the contracts forequals
andhashCode
and returns anEqualsVerifierReport
with the results of the verification.report
(boolean showUrl) Performs the verification of the contracts forequals
andhashCode
and returns anEqualsVerifierReport
with the results of the verification.Suppresses warnings given byEqualsVerifier
.Signals thatgetClass
is used in the implementation of theequals
method, instead of aninstanceof
check.void
verify()
Performs the verification of the contracts forequals
andhashCode
and throws anAssertionError
if there is a problem.withCachedHashCode
(String cachedHashCodeField, String calculateHashCodeMethod, T example) Signals that T caches its hashCode, instead of re-calculating it each time thehashCode()
method is called.withFieldnameToGetterConverter
(Function<String, String> converter) Determines how a getter name can be derived from a field name.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 theequals
contract.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, andEqualsVerifier
therefore 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 theequals
contract.withPrefabValues
(Class<S> otherType, S red, S blue) Adds prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself.withRedefinedSubclass
(Class<? extends T> subclass) Supplies a reference to a subclass of T in whichequals
is overridden.Signals that T is part of an inheritance hierarchy whereequals
is overridden.Signals that all internal caches need to be reset.
-
Constructor Details
-
SingleTypeEqualsVerifierApi
Constructor.- Parameters:
type
- The class for which theequals
method should be tested.
-
SingleTypeEqualsVerifierApi
public SingleTypeEqualsVerifierApi(Class<T> type, EnumSet<Warning> warningsToSuppress, FactoryCache factoryCache, boolean usingGetClass, Function<String, String> converter) Constructor.- Parameters:
type
- The class for which theequals
method should be tested.warningsToSuppress
- A list of warnings to suppress inEqualsVerifier
.factoryCache
- Factories that can be used to create values.usingGetClass
- WhethergetClass
is used in the implementation of theequals
method, instead of aninstanceof
check.converter
- A function that converts from field name to getter name.
-
-
Method Details
-
suppress
Suppresses warnings given byEqualsVerifier
. SeeWarning
to see what warnings can be suppressed.- Specified by:
suppress
in interfaceEqualsVerifierApi<T>
- Parameters:
warnings
- A list of warnings to suppress inEqualsVerifier
.- Returns:
this
, for easy method chaining.
-
withPrefabValues
Adds prefabricated values for instance fields of classes that EqualsVerifier cannot instantiate by itself.- Specified by:
withPrefabValues
in 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.
-
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:
withGenericPrefabValues
in 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:
withGenericPrefabValues
in 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 thatgetClass
is used in the implementation of theequals
method, instead of aninstanceof
check.- Specified by:
usingGetClass
in 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:
withFieldnameToGetterConverter
in 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 theequals
contract.EqualsVerifier
will 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.
- Parameters:
fields
- Fields that should be ignored.- Returns:
this
, for easy method chaining.
-
withOnlyTheseFields
Signals that all given fields, and only the given fields, are relevant for theequals
contract.EqualsVerifier
will 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.
-
withNonnullFields
Signals that all given fields can never be null, andEqualsVerifier
therefore 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.
-
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
@Nonnull
annotation. However, if this method is called with aNonnull.class
parameter, the null verifications will be performed after all.- Parameters:
annotations
- Annotations to ignore.- Returns:
this
, for easy method chaining.
-
withRedefinedSuperclass
Signals that T is part of an inheritance hierarchy whereequals
is overridden. Call this method if T has overriddenequals
andhashCode
, and one or more of T's superclasses have as well.T itself does not necessarily have to have subclasses that redefine
equals
andhashCode
.- Returns:
this
, for easy method chaining.
-
withRedefinedSubclass
Supplies a reference to a subclass of T in whichequals
is overridden. Calling this method is mandatory ifequals
is not final and a strong verification is performed.Note that, for each subclass that overrides
equals
,EqualsVerifier
should 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.- 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,
withCachedHashCode
can not be used whenWarning.NONFINAL_FIELDS
is 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.
-
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.- See Also:
-
withResetCaches
Signals that all internal caches need to be reset. This is useful when the test framework uses multiple ClassLoaders to run tests, causingClass
instances that would normally be equal, to be unequal, because their ClassLoaders don't match.- Specified by:
withResetCaches
in interfaceEqualsVerifierApi<T>
- Returns:
this
, for easy method chaining.
-
verify
public void verify()Performs the verification of the contracts forequals
andhashCode
and throws anAssertionError
if there is a problem.- Throws:
AssertionError
- If the contract is not met, or ifEqualsVerifier
's preconditions do not hold.
-
report
Performs the verification of the contracts forequals
andhashCode
and returns anEqualsVerifierReport
with the results of the verification.- Returns:
- An
EqualsVerifierReport
that indicates whether the contract is met and whetherEqualsVerifier
's preconditions hold.
-
report
Performs the verification of the contracts forequals
andhashCode
and returns anEqualsVerifierReport
with the results of the verification.- Parameters:
showUrl
- Whether or not to show the url at the end of the error message.- Returns:
- An
EqualsVerifierReport
that indicates whether the contract is met and whetherEqualsVerifier
's preconditions hold.
-