Issue
Edit II
Basically, the question is if there's an AssertJ (preferred) or JUnit assertion for:
objA == objB
Edit I
My class under test (CUT) extends JAXB's href="https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html" rel="nofollow noreferrer">XmlAdapter
. When unmarshalling a XML file, it should guarantee that equal objects exist exactly once. In order to verify this, my test currently looks like this (in the example the standard ctor creates equal objects):
MyType obj = cut.unmarshal(new MyType());
assertThat(cut.unmarshal(new MyType()) == obj).isTrue();
Is there a way to explicitly assert identity with AssertJ or JUnit?
Original Post
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?
Solution
If you are using AssertJ, you can use the isSameAs
method to compare object identity:
assertThat(cut.foo()).isSameAs(obj);
Answered By - chrylis -cautiouslyoptimistic-
Answer Checked By - Mary Flores (JavaFixing Volunteer)