简体   繁体   中英

Why do I get a NoSuchMethodError in my java unit tests but the IDE doesn't show classpath problems?

I have a unit test that uses some methods of the com.google.guava API, that is included as sub-dependencies in some other dependencies I declared in my pom.xml. The IDE, in my case eclipse, didn't show any problems.

But when I run the test I get the error:

com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.base.Objects.firstNonNull(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

I have seen other users who fixed this problem by excluding guava like this:

<dependency>
  <groupId>com.atlassian.activeobjects</groupId>
  <artifactId>activeobjects-test</artifactId>
  <version>${ao.version}</version>
  <exclusions>
    <exclusion>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
    </exclusion>
  </exclusions>
  <scope>test</scope>
</dependency>

but that didn't help. Instead it also caused classpath errors in my non-test code. What can I do instead?

I found out that if you explicitly add the dependency for guava in your pom, the error is gone!

So I just added this dependency:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

In my case, this error was fixed by using a newer version of activeobjects (2.0.0 --> 3.0.0)

<properties>
  <ao.version>3.0.0</ao.version>
</properties>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM