3

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 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?

beatngu13
  • 7,201
  • 6
  • 37
  • 66

1 Answers1

6

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

assertThat(cut.foo()).isSameAs(obj);
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152