0

Seems very stupid, but I can't find good solution to this (I guess very basic) problem: I have an list with elements that can not be checked by equals. Instead, I want to assert items state by their properties, and I want to do it ignoring item order.

assertThat(list.get(xx)) doesn't fit, as it assumes knowing exact order. assertThat(list).containsInAnyOrder(xx, yyy, ...) doesn't fit as it assumes being able to compare objects by equals(). What I miss is something like assertThat(list).containsInAnyOrder( Consumer<ObjectAssert<T>>...itemAssertions ). As a bonus, in my case I already have custom Assertion for my object type T, but I guess that would require really flexible API to use that for list items.

What do I miss?

Askar Kalykov
  • 2,553
  • 1
  • 22
  • 43

1 Answers1

1

You can try usingRecursiveFieldByFieldElementComparator chained with containsExactlyInAnyOrder assertion.

Joel Costigliola
  • 6,308
  • 27
  • 35
  • Thank you. From the javadoc, `usingRecursiveFieldByFieldElementComparator` allows comparing fields by equals, but what I need is generic (ideally) or startsWith() and contains() comparison on certain fields. – Askar Kalykov Mar 20 '19 at 08:20
  • [usingElementComparator](http://joel-costigliola.github.io/assertj/core-8/api/org/assertj/core/api/AbstractListAssert.html#usingElementComparator-java.util.Comparator-) should help but you have to write the comparator yourself – Joel Costigliola Mar 21 '19 at 08:53