java Hamcrest 断言那模棱两可?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7955351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:10:33  来源:igfitidea点击:

Hamcrest assertThat ambiguous?

javajunit4hamcrest

提问by MartinL

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

我从一所大学得到了一些示例代码,导入了项目并尝试运行测试:方法 assertThat(Integer, Matcher) 对于 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:

在我滚动到 assertThat 后,我​​收到以下消息:

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.

我搜索了 google 和 stackoverflow,但找不到任何有同样问题的人......请帮忙。

EDIT1:

编辑1:

Solution:

解决方案:

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

导入静态 org.junit.Assert.*; // 删除这一行

回答by pholser

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

两者org.junit.Assertorg.hamcrest.MatcherAssert声明assertThat(T, Matcher<T>)。选择静态导入一个或另一个,但不是两个,你应该没问题。

回答by Dave Newton

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

造成这种情况的一般原因有两个:不合格的静态导入 ( import static blah.*) 或路径上存在多个版本的 hamcrest。

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.

您可以通过使用长格式is(equalTo(3))(有点怀疑),剔除静态导入等来解决它。

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

您使用的框架也很重要。