AssertJ's assertThat(...).isEqualTo()
method compares using .equals()
by default. This is often the right choice, but sometimes you really do want to compare by exact object identity / referential identity instead ("double equals" ==
in Java, "triple equals" ===
in Kotlin).
Is there an idiomatic way to force AssertJ to do this kind of comparison, e.g. an assertion method for it that I don't know about?
The best I've been able to do is work around it by doing the comparison without AssertJ, then merely using it to check the resulting bool:
Kotlin:
assertThat(obj1 === obj2).isTrue()
Java:
assertThat(obj1 == obj2).isTrue();
As I already hinted at, this isn't ideal because a) it looks inconsistent compared to the other assertions and b) the resulting error messages when the assertion fails just say "expected true, got false" which isn't as nice as showing the differing objects in question (even if they only differ by their memory locations, not their contents).