Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Idiomatic way to force AssertJ to compare by identity?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

>Solution :

It already provided methods for such comparing references stuff which are :

assertThat(obj1).isSameAs(obj2);

or

assertThat(obj1).isNotSameAs(obj2);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading