Reputation: 3308
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
Upvotes: 13
Views: 12193
Reputation: 4937
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.
Upvotes: 30
Reputation: 160171
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.
Upvotes: 4