Generics 汉克雷斯特';s anything()不';不编译

Generics 汉克雷斯特';s anything()不';不编译,generics,compiler-errors,hamcrest,Generics,Compiler Errors,Hamcrest,我是不是遗漏了什么 @Test public void testAnything(){ Random random = new Random(); assertThat(random.nextInt(), is(equalTo(anything()))); } 这不能编译。Eclipse抱怨“MatcherAssert类型中的方法assertThat(T,Matcher)不适用于参数(int,Matcher>)” 在使用任何东西时,我是否遗漏了什么?我过去用过其他的Hamcre

我是不是遗漏了什么

@Test
public void testAnything(){
    Random random = new Random();
    assertThat(random.nextInt(), is(equalTo(anything())));
}
这不能编译。Eclipse抱怨“MatcherAssert类型中的方法assertThat(T,Matcher)不适用于参数(int,Matcher>)”


在使用任何东西时,我是否遗漏了什么?我过去用过其他的Hamcrest方法。。。那么这有什么不同呢?

这不是
equalTo
的工作原理。它在内部调用
Object#equals(Object)
,并且必须传递
anything()
。那没有道理。只要省略它,它就会起作用:

Random random = new Random();
assertThat(random.nextInt(), is(anything()));