2012-12-12 91 views
1

我在这里错过了什么吗?Hamcrest的任何东西()不编译

@Test 
public void testAnything(){ 
    Random random = new Random(); 
    assertThat(random.nextInt(), is(equalTo(anything()))); 
} 

这不能编译。 Eclipse会抱怨:“MatcherAssert类型中的方法assertThat(T,Matcher)不适用于参数(int,Matcher>)”

是否有我错过了使用任何()的东西?过去我使用过其他Hamcrest方法......所以这有什么不同?

回答

2

这不是equalTo的工作方式。它在内部调用Object#equals(Object),并且必须通过anything()。这没有意义。只是省略它而已:

Random random = new Random(); 
assertThat(random.nextInt(), is(anything())); 
相关问题