JUnit抛出java.lang.NoSuchMethodError对于com.google.common.collect.Iterables.tryFind

dpr*_*ice 6 java junit nosuchmethoderror guava

我正在使用Google Guava v13.0但是当我使用包含tryFind的代码运行JUnit测试时,我收到以下消息:

java.lang.NoSuchMethodError: com.google.common.collect.Iterables.tryFind(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Lcom/google/common/base/Optional;
Run Code Online (Sandbox Code Playgroud)

这似乎只发生在JUnit测试中,因为生产代码运行时没有问题.我正在使用Intellij IDEA v11.1.3并可以导航到guava JAR文件以在com.google.common.collect.Iterable.class中找到tryFind.

我见过类似的帖子,但我不确定这与JUnit有什么关系.关于我的问题可能是什么想法?

Col*_*inD 18

除了您尝试使用的较新版本之外,这种错误通常是由类路径上的旧版Guava(甚至是google-collections)引起的.在运行测试时尝试检查类路径上的内容.


Sea*_*oyd 13

与Colin的答案一起,这是检测内容加载位置的好方法:

System.out.println(
    Iterables.class.getProtectionDomain().getCodeSource().getLocation()
);
Run Code Online (Sandbox Code Playgroud)

这应该打印出您正在使用的番石榴(或gc)版本的路径.

  • 确实非常有帮助!我在同时加载 google-collections-1.0.jar 和 guava-20.0.jar 时遇到了类似的问题。我必须删除 google-collections-1.0.jar (2认同)