【问题标题】:I can't compile hamcrest hasKey() method我无法编译 hamcrest hasKey() 方法
【发布时间】:2011-07-28 21:31:55
【问题描述】:

这是代码:

Map<Foo, String> map;
org.hamcrest.MatcherAssert.assertThat(map, 
  org.hamcrest.Matchers.hasKey(new Foo()));

这就是编译器的意思:

cannot find symbol method assertThat(java.util.Map<Foo,java.lang.String>,
org.hamcrest.Matcher<java.util.Map<Foo,java.lang.Object>>)

为什么以及我能做什么?

【问题讨论】:

    标签: java generics junit hamcrest


    【解决方案1】:

    我怀疑你需要类似的东西:

    MatcherAssert.assertThat(map, Matchers.<Foo, String>hasKey());
    

    这样您就可以为hasKey 方法调用指定值类型。虽然看起来很丑,但我有点惊讶类型推断对你没有帮助......

    【讨论】:

    • 这个问题困扰了我多年。年!谢谢,乔恩。
    【解决方案2】:

    听起来你遇到了和我一样的错误。这是在 Hamcrest > 1.1 中吗?他们在 1.1 和 1.2 之间更改了匹配器上的泛型。我在这里提交了一个 Hamcrest 错误: http://code.google.com/p/hamcrest/issues/detail?id=143

    但事实证明,这实际上是编译器中的一个错误,在 JDK 6 中无法修复,但在 7 中已经修复: http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=72ce99618021685c3570069c8f60b?bug_id=7034548

    正如 Jon 所提到的,有几种方法可以解决这个问题,但它们都破坏了 Hamcrest 漂亮、流畅的界面。

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2016-12-25
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      相关资源
      最近更新 更多