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

2

8

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?

Meggs answered 20/8, 2012 at 16:1 Comment(5)
Do you use maven? Maybe the jar isn't in scope?Burdine
This seems more likely to be a classpath or build framework problem than a Guava problem.Candicandia
We do not use Maven. All libraries are manually added to the project.Meggs
@atamanroman; AFAIK, missing jar can never lead to NoSuchMethodError as you first get a NoSuchClassException.Anhydrous
@Anhydrous true, seems like there are different versions of guava includedBurdine
P
21

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.

Pox answered 20/8, 2012 at 17:45 Comment(2)
This was definitely the issue and @Sean's code below showed me where the issue was: javaee-reference-6.0-glassfish-3.1.2.jar. Any idea on how to get around this?Meggs
I had a dependencies to guava-collection version r03 in one of the dependencies of my project.Abandoned
P
15

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.

Philender answered 20/8, 2012 at 20:30 Comment(4)
@Pox answered correctly but this is what helped me find it. It's in javaee-reference-6.0-glassfish-3.1.2.jar. Any idea on how to get around this?Meggs
@Meggs get rid of that jee-Jar. if you use glassfish in production, it will provide all that. for the test scope, don't include the full jee bundle, only what you needPhilender
This was very helpful. Iterables was buried down in a MyEclipse plugin!Benthamism
Very helpful indeed! I had a similar problem with google-collections-1.0.jar and guava-20.0.jar loaded at the same time. I had to remove the google-collections-1.0.jarHoggish

© 2022 - 2024 — McMap. All rights reserved.