【问题标题】:Hamcrest's anything() doesn't compileHamcrest 的任何东西()都无法编译
【发布时间】:2012-12-12 06:05:06
【问题描述】:

我错过了什么吗?

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

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

使用anything() 有什么我错过的吗?我过去使用过其他 Hamcrest 方法……那么这有什么不同?

【问题讨论】:

    标签: generics compiler-errors hamcrest


    【解决方案1】:

    equalTo 不是这样工作的。它在内部调用Object#equals(Object),并且必须通过anything()。那没有意义。忽略它就可以了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2013-06-29
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2012-03-21
      相关资源
      最近更新 更多