Vitali Plagov
Vitali Plagov

Reputation: 762

Why Hamcrest's containsInAnyOrder matcher accepts array and not a list?

I'm using Hamcrest's containsInAnyOrder matcher when asserting REST response using Rest Assured. Here's an example of my assertion:

assertThat(
        body.jsonPath().getList("zones.name"),
        containsInAnyOrder(values.getName().toArray()));

First argument returns a List. Second argument (values.getName()) also returns a List. But Intellij IDEA shows an error on a mactcher: Unchecked generics array creation for varargs parameter. When I run this assertions, I get java.lang.AssertionError. When I convert second argument to an array, like values.getName().toArray(), I get everything working as expected.

So I can't understand why comparing a List with a List doesn't work, but List with an array does? Why do I need to convert the second argument to an array?

Upvotes: 3

Views: 3342

Answers (2)

Saljack
Saljack

Reputation: 2344

Because there is Collection of Matchers:

public static <T> Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers)

You could use something like containsInAnyOrder(equalTo("bar"), equalTo("foo")) but it is not so handy.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311393

containsInAnyOrder accepts a T....

When you pass a List, you aren't comparing the elements in the body.jsonPath().getList("zones.name") to the elements in the values.getName(), but to a single-element array that contains the list itself. Since a string cannot be equal to a list, the assertion fails.

Upvotes: 1

Related Questions