0

I compare two objects using assertJ. I have actual object which is output from our application and expected object. This object has field which is an array of other object but elements in this array are present in random order so sometimes test passes but also often is failed because incorrect elements of array are compared.

What I'm doing I get arrays from both objects sort them and then set to objects but this looks ugly and uses 7 lines of code.

Comparator.comparing(Value::getId)

Value[] actualOrderChildren = actualOrder.getChildren();
Arrays.sort(actualOrderChildren, comparing);
actualOrder.setChildren(actualOrderChildren);

Value[] expectedOrderChildren = expectedOrder.getChildren();
Arrays.sort(expectedOrderChildren, comparing);
expectedOrder.setChildren(expectedOrderChildren);

assertThat(actualOrder).isEqualToComparingFieldByFieldRecursively(expectedOrder);

Is there any better looking solution using assertj fluent assertions ?

kris82pl
  • 977
  • 3
  • 12
  • 21
  • Have a look at [this question](https://stackoverflow.com/questions/22807328/assertequals-2-lists-ignore-order) – Aleks G Aug 07 '19 at 12:50

1 Answers1

1

containsExactlyInAnyOrder is probably what your are looking for.

assertThat(actualOrder.getChildren()).containsExactlyInAnyOrder(expectedOrder.getChildren());
Sebastien
  • 5,506
  • 4
  • 27
  • 37