[英]Hamcrest assertThat ambiguous?
我从大学获得了一些样本代码,导入了项目并尝试运行测试:方法assertThat(Integer,Matcher)对于MyClass类型是不明确的
每个断言都标记为红色,并显示相同的错误消息,因此我尝试编写描述问题的最简单的测试:
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));
}
滚动断言后,我收到以下消息:
The method assertThat(Integer, Matcher<Integer>) is ambiguous for the type MyClass
我搜索了谷歌和stackoverflow但找不到任何有同样问题的人...请帮忙。
EDIT1:
解:
import static org.junit.Assert。*; //删除这一行
org.junit.Assert
和org.hamcrest.MatcherAssert
声明assertThat(T, Matcher<T>)
。 选择静态导入一个或另一个,但不是两个,你应该没问题。
这有两个一般原因,不合格的静态导入( import static blah.*
),或路径上的多个版本的hamcrest。
您可以通过使用long-form is(equalTo(3))
(有点怀疑),剔除静态导入等来绕过它。
您使用它的哪个框架也很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.