Question

I got some samplecode from a college, imported the project and try to run the Tests: The method assertThat(Integer, Matcher) is ambiguous for the type MyClass

Every assertThat is marked red with the same error-message so i tried to write the simpliest test which describes the problem:

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

@Test
public void whenAssertThatThenItIsAmbiguous() {
    List<String> list = Arrays.asList("A", "B", "C");
    assertThat(list.size(), is(3));
}

after I scroll over assertThat I get the following message:

The method assertThat(Integer, Matcher<Integer>) is ambiguous for the type MyClass

I searched google and stackoverflow but couldn't find anybody with the same problem... Please help.

EDIT1:

Solution:

import static org.junit.Assert.*; // delete this line

Was it helpful?

Solution

Both org.junit.Assert and org.hamcrest.MatcherAssert declare assertThat(T, Matcher<T>). Choose to static-import one or the other, but not both, and you should be OK.

OTHER TIPS

There's two general causes for this, unqualified static imports (import static blah.*), or multiple versions of hamcrest on the path.

You may be able to get around it by using the long-form is(equalTo(3)) (kind of doubt it), culling your static imports, etc.

Which framework you're using it with can matter, too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top