首页 > 解决方案 > 为什么在 IDEA 而不是 Eclipse 中 Hamcrest 的 AllOf() 可以?

问题描述

以下测试代码在IDEA中可以编译成功,而在Eclipse中无法编译。来自 Eclipse 的消息是:

Matchers 类型中的方法 allOf(Matcher<? super T>, Matcher<? super T>) 不适用于参数 (Matcher<Object[]>, Matcher<Double[]>)。

我认为 allOf() 的第一个参数的类型推断为 Matcher<Object>,而第二个参数的类型推断为 Matcher<Double>。但是 allOf() 要求两个参数相同。如果是这样,为什么 Eclipse 会以这种方式进行推理?

public class HamTest {
    @Test
    public void test() {
        Double s[] = { 23.0, 65.0 };
        assertThat(s, allOf(arrayWithSize(2), arrayContaining(lessThan(25.0), greaterThan(30.0))));
    }
}

标签: eclipsehamcrest

解决方案


我知道了。我认为 ECJ 编译器错误地将 Matcher<E[]> 实例化为 Matcher<Object[]> 而不是 Matcher<Double[]> 用于 arrayWithSize()。于是修改了arrayWithSize()的调用,编译成功。

Matchers.<Double>arrayWithSize(2)

推荐阅读