Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit compare objects in collections except specified fields in contained objects

JUnit tests...

It needs to compare 2 single elements excepting some fields. I can use assertj for this:

Assertions.assertThat(actual).isEqualToIgnoringGivenFields(except, "id", "innerCollection");

and it works good. But it's not enough. I need to compare inner collections also. I know that only ids are different in inner collection. Is it possible to compare ones like

Assertions.assertThat(to.getInnerCollection()).isEqualToIgnoringGivenFields(from.getInnerCollection()
, {except fields in collection elements});
like image 479
Sergii Avatar asked Jan 26 '26 05:01

Sergii


1 Answers

In this case, I would rather consider to create your own custom matcher.

That is actually a straight-forward task; you can find a detailed example here.

like image 94
GhostCat Avatar answered Jan 27 '26 19:01

GhostCat



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!