beatngu13 beatngu13 - 1 month ago 15
Java Question

Assert identity with AssertJ or JUnit

My class under test (CUT) has a method (e.g.

foo
) which should guarantee that returned objects—that are equal—exist exactly once. Currently, I'm using the following assert statement:

assertThat(cut.foo() == obj).isTrue();


Is there a way to explicitly assert identity with AssertJ or JUnit?

Answer

If you are using AssertJ, you can use the isSameAs method to compare object identity:

assertThat(cut.foo()).isSameAs(obj);