JUnit throws java.lang.NoSuchMethodError For com.google.common.collect.Iterables.tryFind

旧巷老猫 提交于 2019-11-28 04:22:05

问题


I am using Google Guava v13.0 but when I run a JUnit test with code containing tryFind, I get this message:

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

This only seems to happen with JUnit tests as when the production code runs there are no problems. I am using Intellij IDEA v11.1.3 and can navigate into the guava JAR file to find tryFind in the com.google.common.collect.Iterable.class.

I've seen similar posts, but I'm not sure how this relates to JUnit. Any ideas on what my issue might be?


回答1:


This sort of error is usually caused by having an older version of Guava (or even google-collections) on the classpath in addition to the newer version you're trying to use. Try to check what's on the classpath when running your test.




回答2:


Go with Colin's answer, here's a nice way to detect where the stuff is loaded:

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

This should print out the path to the guava (or g-c) version you are using.



来源:https://stackoverflow.com/questions/12041111/junit-throws-java-lang-nosuchmethoderror-for-com-google-common-collect-iterables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!