Matches IsIterableContainingInAnyOrder have two overloads for the static factory containsInAnyOrder method (both have the return type Matcher<java.lang.Iterable<? extends T>> ):
containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers)containsInAnyOrder(Matcher<? super T>... itemMatchers)
Now consider the following program:
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; import java.util.Arrays; import org.junit.Test; public class SomeTest { @SuppressWarnings("unchecked") @Test public void foo() { assertThat(Arrays.asList("foo","bar"), containsInAnyOrder(equalTo("foo"), equalTo("bar"))); } }
When doing this as a JUnit test, it passes as expected. It uses the second containsInAnyOrder overload shown above.
Now when I change the statement to this (which exactly matches the example given in the documentation of the first overload ):
assertThat(Arrays.asList("foo","bar"), containsInAnyOrder(Arrays.asList(equalTo("foo"), equalTo("bar")))); ^^^^^^^^^^^^^^
it no longer compiles, because now the compiler prints the return type containsInAnyOrder to
Matcher<Iterable<? extends List<Matcher<String>>>>
It seems that the compiler is still picking a second overload. If he used the first, the example should work. Why is he acting like that? How can I do this job?
I am using Hamcrest 1.3 and Oracle Java 1.7.
source share