JUnit的抛出java.lang.NoSuchMethodError对于com.google.co

2019-06-27 04:52发布

我使用谷歌番石榴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;

这似乎只与JUnit测试发生在生产代码运行没有问题。 我使用的IntelliJ IDEA v11.1.3及可以导航到番石榴JAR文件找到tryFind在com.google.common.collect.Iterable.class。

我已经看到了类似的帖子,但我不知道如何这涉及到JUnit的。 什么我的问题可能是任何想法?

Answer 1:

这种错误通常是由除了你试图使用较新的版本具有的类路径上的旧版本番石榴(甚至谷歌的集合)造成的。 尝试检查运行测试时,什么是类路径上。



Answer 2:

去科林的答案,这里是一个很好的方法来检测其中的东西是装:

System.out.println(
    Iterables.class.getProtectionDomain().getCodeSource().getLocation()
);

这应该打印出来的路径,您使用的是番石榴(或GC)的版本。



文章来源: JUnit throws java.lang.NoSuchMethodError For com.google.common.collect.Iterables.tryFind