How to assert that two Lists<String> are equal, ignoring order
Asked Answered
S

4

22

I am using AssertJ and I am trying to assert that two List<String> contain same strings, ignoring the order.

List<String> expected = Arrays.asList("Something-6144-77.pdf", "d-6144-77.pdf", "something-6144-78.pdf", "Something-6144-8068.pdf");
List<String> actual = new ArrayList<String>();

assertThat(actual.size()).isEqualTo(expected.size());

// This line gives the error: "The method containsExactlyInAnyOrder(String...) in the type ListAssert<String> is not applicable for the arguments (List<String>)"
assertThat(actual).containsExactlyInAnyOrder(expected);

How can I fix the compilation error below that is appearing when trying to use containsExactlyInAnyOrder()?

"The method containsExactlyInAnyOrder(String...) in the type ListAssert is not applicable for the arguments (List)"

Stonwin answered 27/3, 2017 at 20:46 Comment(0)
I
13

The error message gives you the solution:

The method containsExactlyInAnyOrder(String...)

String... is a any number of strings but can be passed as an array as well:

assertThat(actual).containsExactlyInAnyOrder((String[]) expected.toArray(new String[expected.size()]));

The cast is necessary here and that code is given under the assumption that the expected element is created different than in your example, as it doesn't make sense to convert an array to a list and back.

Here some documentation to varargs (Arbitrary number of arguments, the ...): https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Infinite answered 27/3, 2017 at 20:51 Comment(0)
K
18

Both the answers (by jlordo and by dasblinkenlight) work, but are workarounds rather than the correct way to do it.

There is a method in the AssertJ library for specifically checking if a List contains all values, regardless of order, in another Iterable. It is called containsOnlyElementsOf():

public SELF containsOnlyElementsOf(Iterable<? extends ELEMENT> iterable)

Same semantic as ObjectEnumerableAssert.containsOnly(Object[]) : verifies that actual contains all the elements of the given iterable and nothing else, in any order.

Example :

Iterable<Ring> rings = newArrayList(nenya, vilya);

// assertion will pass
assertThat(rings).containsOnlyElementsOf(newLinkedList(nenya, vilya)) .containsOnlyElementsOf(newLinkedList(nenya, nenya, vilya, vilya));

// assertion will fail as actual does not contain narya
assertThat(rings).containsOnlyElementsOf(newLinkedList(nenya, vilya, narya));
// assertion will fail as actual contains nenya assertThat(rings).containsOnlyElementsOf(newLinkedList(vilya));

So, this method is the one you should use, like below. There is no need to cast or transform your List to an Array.

assertThat(actual).containsOnlyElementsOf(expected);

As a side note, your assertion on the size of the list is redundant:

assertThat(actual.size()).isEqualTo(expected.size());

This is already covered in the assertion that the lists contain the same elements.

Finally, if you do need to assert that a list has a specific site, AssertJ has a built-in method for this (hasSameSizeAs()):

assertThat(actual).hasSameSizeAs(expected);
Kozlowski answered 29/5, 2017 at 10:56 Comment(0)
I
13

The error message gives you the solution:

The method containsExactlyInAnyOrder(String...)

String... is a any number of strings but can be passed as an array as well:

assertThat(actual).containsExactlyInAnyOrder((String[]) expected.toArray(new String[expected.size()]));

The cast is necessary here and that code is given under the assumption that the expected element is created different than in your example, as it doesn't make sense to convert an array to a list and back.

Here some documentation to varargs (Arbitrary number of arguments, the ...): https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Infinite answered 27/3, 2017 at 20:51 Comment(0)
B
8

In newer versions of assertj there is a containsExactlyInAnyOrderElementsOf for exactly this purpose. Your example boils down to

assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
Baudin answered 29/10, 2019 at 12:24 Comment(1)
And if that method is not available, make sure you have actually used import static org.assertj.core.api.Assertions.assertThat; and not one of its cousins from other packages.Gave
H
4

Since the method takes String..., you should pass an array instead of a list:

String[] expected = new String[] {
    "Something-6144-77.pdf"
,   "d-6144-77.pdf"
,   "something-6144-78.pdf"
,   "Something-6144-8068.pdf"
};

or call it with the list of items inlined:

assertThat(actual).containsExactlyInAnyOrder(
    "Something-6144-77.pdf"
,   "d-6144-77.pdf"
,   "something-6144-78.pdf"
,   "Something-6144-8068.pdf"
);
Hunt answered 27/3, 2017 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.