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?
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.
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