Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert identity with AssertJ or JUnit

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?

like image 341
beatngu13 Avatar asked Aug 11 '26 02:08

beatngu13


1 Answers

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

assertThat(cut.foo()).isSameAs(obj);
like image 135
chrylis -cautiouslyoptimistic- Avatar answered Aug 13 '26 14:08

chrylis -cautiouslyoptimistic-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!