Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 227
dprice Avatar asked Aug 20 '12 16:08

dprice


2 Answers

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.

like image 93
ColinD Avatar answered Jan 31 '23 22:01

ColinD


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.

like image 35
Sean Patrick Floyd Avatar answered Jan 31 '23 22:01

Sean Patrick Floyd