I have a class like this:
class Thing(val name: String) {
internal var complexObject: ComplexType? = null
override fun equals(other: Any?): Boolean {
return other is Thing && name == other.name
}
}
And a test like this:
import nl.jqno.equalsverifier.EqualsVerifier
import org.junit.Test
class ThingTest {
@Test
fun `test equals and hashCode`() {
EqualsVerifier.forClass(Thing::class.java)
.verify()
}
}
Now I want to completely ignore complexObject
in EqualsVerifier. It's a type it cannot instantiate, and it asks me to provide a prefab object (withPrefabValues
) for it. This would work, but my Thing
is used in a lot of places and other people write a lot of tests for classes that contain a Thing
, and the complexObject
should be none of their concern. Comparisons of this object are not necessary for these downstream tests to work.
Can I somehow mark complexObject
as always being null
for the purposes of EqualsVerifier?
Ideally without touching all tests which verify objects which transitively use a Thing
, but if it's only possible test by test, then this would already be an improvement over having to pass a prefab object every time (because withPrefabValues
needs at least one real object that is not null).
Creator of EqualsVerifier here.
Unfortunately, what you ask for isn't possible. Even for fields that aren't used directly by equals
and hashCode
, EqualsVerifier still wants to assign values to them, to prevent potential NullPointerExceptions in unrelated parts of the code that EqualsVerifier might touch.
As a workaround, perhaps you can use a mocking framework to create mocks of ComplexType
, and feed those to withPrefabValues
. (Also, as @Leviathan suggests, perhaps some re-architecting will also help. Does complex logic belong in a type that also needs to have an equals
method? Maybe you can extract an interface for ComplexType
? That might make other tests easier to do, too.)