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

Catfish :

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)"

jlordo :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=455725&siteId=1