简体   繁体   中英

NoSuchMethodError: javassist.CtClass.getDeclaredClasses()

I have been struggling with this error for two days now. I am in a spring, maven project, using Intellij.

I have tried every question I found here in SO and nothing. I have tried with a bunch of versions and nothing.

I am completely stuck by the following exception:

java.lang.NoSuchMethodError: javassist.CtClass.getDeclaredClasses()[Ljavassist/CtClass;

    at org.powermock.core.transformers.javassist.ConstructorsMockTransformer.transform(ConstructorsMockTransformer.java:50)
    at org.powermock.core.transformers.javassist.AbstractJavaAssistMockTransformer.transform(AbstractJavaAssistMockTransformer.java:40)
    at org.powermock.core.transformers.support.DefaultMockTransformerChain.transform(DefaultMockTransformerChain.java:43)
    at org.powermock.core.classloader.MockClassLoader.transformClass(MockClassLoader.java:184)
    at org.powermock.core.classloader.javassist.JavassistMockClassLoader.defineAndTransformClass(JavassistMockClassLoader.java:102)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:174)
    at org.powermock.core.classloader.MockClassLoader.loadClassByThisClassLoader(MockClassLoader.java:102)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:147)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:98)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:154)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:47)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:107)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:69)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:36)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Here is my code:

test

package com.secondrain.utils;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(CompanyManager.class)
public class CompanyTest {

  @Test
  public void test() {}

}

Everything works perfectly if I remove PowerMock:

package com.secondrain.utils;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class CompanyTest {

  @Test
  public void test() {}

}

PowerMock dependencies

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-core</artifactId>
      <version>2.0.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>2.0.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito2</artifactId>
      <version>2.0.2</version>
      <scope>test</scope>
    </dependency>

Dependency tree

[INFO] ------------------------------------------------------------------------
[INFO] Building Workbench 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Workbench ---
[INFO] com.secondrain:Workbench:jar:1.0.0
[INFO] +- thirdparty.secondrain:Objects:jar:jdk6:1.1:compile
[INFO] |  +- thirdparty.secondrain:fr-common-db:jar:jdk6:1.0:compile
[INFO] |  |  +- thirdparty.secondrain:fr-common-utils:jar:jdk6:1.1:compile
[INFO] |  |  |  +- commons-digester:commons-digester:jar:1.8.1:compile
[INFO] |  |  |  +- thirdparty.xml-apis:xml-apis:jar:0:compile
[INFO] |  |  |  +- thirdparty:lingpipe:jar:3.1.1:compile
[INFO] |  |  |  +- thirdparty.activation:activation:jar:1.1.1:compile
[INFO] |  |  |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  |  |  +- opensymphony:quartz:jar:1.5.0:compile
[INFO] |  |  |  +- isorelax:isorelax:jar:20030108:compile
[INFO] |  |  |  +- thirdparty.javamail:mail:jar:1.3.1:compile
[INFO] |  |  |  +- com.google.guava:guava:jar:14.0.1:compile
[INFO] |  |  |  +- commons-io:commons-io:jar:2.1:compile
[INFO] |  |  |  +- thirdparty:jce:jar:1.2.2:compile
[INFO] |  |  |  +- thirdparty:sunjce_provider-cv:jar:1.0:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.5.1:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-core:jar:2.5.1:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-databind:jar:2.5.1:compile
[INFO] |  |  +- thirdparty.secondrain:replication:jar:1.0:compile
[INFO] |  |  +- relaxngDatatype:relaxngDatatype:jar:20020414:compile
[INFO] |  |  +- xsdlib:xsdlib:jar:20030225:compile
[INFO] |  |  +- msv:msv:jar:20030225:compile
[INFO] |  |  +- commons-dbutils:commons-dbutils:jar:1.0:compile
[INFO] |  |  +- commons-dbcp:commons-dbcp:jar:1.2.2:compile
[INFO] |  |  +- org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile
[INFO] |  |  |  +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
[INFO] |  |  |  +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] |  |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- javax.transaction:jta:jar:1.1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:3.3.1.GA:compile
[INFO] |  |  |  \- antlr:antlr:jar:2.7.6:compile
[INFO] |  |  +- org.hibernate:hibernate-jmx:jar:3.3.1.GA:compile
[INFO] |  |  +- org.hibernate:hibernate-cglib-repack:jar:2.1_3:compile
[INFO] |  |  +- org.hibernate:hibernate-c3p0:jar:3.3.1.GA:compile
[INFO] |  |  |  \- c3p0:c3p0:jar:0.9.1:compile
[INFO] |  |  +- org.slf4j:slf4j-api:jar:1.6.6:compile
[INFO] |  |  +- org.slf4j:slf4j-log4j12:jar:1.6.6:compile
[INFO] |  |  \- commons-pool:commons-pool:jar:1.6:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  +- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
[INFO] |  +- poi:poi:jar:3.6-20091214:compile
[INFO] |  +- poi:poi-ooxml:jar:3.6-20091214:compile
[INFO] |  +- poi:poi-ooxml-schemas:jar:3.6-20091214:compile
[INFO] |  \- commons-httpclient:commons-httpclient:jar:3.0.1:compile
[INFO] |     \- commons-codec:commons-codec:jar:1.2:compile
[INFO] +- com.secondrain:Extraction:jar:1.0.0:compile
[INFO] |  +- com.secondrain:ContentProducers:jar:1.0.0:compile
[INFO] |  |  \- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] |  +- thirdparty.secondrain:ImageServiceClient:jar:jdk6:1.0:compile
[INFO] |  |  +- thirdparty.secondrain:HtmlParser:jar:jdk6:1.0:compile
[INFO] |  |  |  +- org.ccil.cowan.tagsoup:tagsoup:jar:1.2:compile
[INFO] |  |  |  \- thirdparty.tika-app:tika-app-1.4Custom:jar:0:compile
[INFO] |  |  +- com.sun.jersey:jersey-client:jar:1.0.2:compile
[INFO] |  |  +- com.sun.jersey:jersey-core:jar:1.0.2:compile
[INFO] |  |  \- javax.ws.rs:jsr311-api:jar:1.0:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |  +- com.google.code.gson:gson:jar:2.2.4:compile
[INFO] |  +- thirdparty.solr.commons:commons-csv:jar:1.0-SNAPSHOT-r609327:compile
[INFO] |  +- thirdparty.jcommon:jcommon:jar:1.0.13:compile
[INFO] |  +- thirdparty.lang:jsonic:jar:1.2.4:compile
[INFO] |  +- org.rhq.helpers:rhq-pluginAnnotations:jar:3.0.4:compile
[INFO] |  +- thirdparty.xalan:xalan:jar:2.5.2:compile
[INFO] |  +- thirdparty.lang:langdetect:jar:0:compile
[INFO] |  +- thirdparty.stanford-corenlp:stanford-corenlp:jar:3.3.1:compile
[INFO] |  +- thirdparty.stanford-corenlpmodel:stanford-corenlpmodel:jar:models:0:compile
[INFO] |  +- org.jsoup:jsoup:jar:1.8.1:compile
[INFO] |  \- net.openhft:chronicle-map:jar:3.9.0:compile
[INFO] |     +- net.openhft:chronicle-core:jar:1.6.2:compile
[INFO] |     +- net.openhft:chronicle-values:jar:1.5.1:compile
[INFO] |     |  +- com.sun.java:tools:jar:1.8.0_231:system
[INFO] |     |  \- com.squareup:javapoet:jar:1.5.1:compile
[INFO] |     +- net.openhft:chronicle-threads:jar:1.6.1:compile
[INFO] |     |  \- net.openhft:affinity:jar:3.0.5:compile
[INFO] |     +- com.intellij:annotations:jar:12.0:compile
[INFO] |     +- net.openhft:chronicle-wire:jar:1.6.2:compile
[INFO] |     +- net.openhft:chronicle-bytes:jar:1.6.1:compile
[INFO] |     +- net.openhft:chronicle-algorithms:jar:1.1.7:compile
[INFO] |     +- net.java.dev.jna:jna:jar:4.2.1:compile
[INFO] |     +- net.java.dev.jna:jna-platform:jar:4.2.1:compile
[INFO] |     +- com.thoughtworks.xstream:xstream:jar:1.4.8:compile
[INFO] |     |  +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] |     |  \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] |     +- org.codehaus.jettison:jettison:jar:1.3.7:compile
[INFO] |     |  \- stax:stax-api:jar:1.0.1:compile
[INFO] |     \- org.ops4j.pax.url:pax-url-aether:jar:2.4.5:compile
[INFO] |        \- org.slf4j:jcl-over-slf4j:jar:1.6.6:compile
[INFO] +- com.secondrain:Tools:jar:1.0.0:compile
[INFO] |  +- thirdparty.secondrain:ContentProcessingFramework:jar:jdk6:1.0:compile
[INFO] |  |  +- displaytag:displaytag-export-poi:jar:1.1:compile
[INFO] |  |  +- org.jboss:jboss-common-core:jar:2.2.13.GA:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-spi:jar:2.0.5.GA:compile
[INFO] |  |  +- org.jboss:staxmapper:jar:1.1.0.Final:compile
[INFO] |  |  +- org.infinispan:infinispan-core:jar:6.0.0.Final:compile
[INFO] |  |  |  +- org.infinispan:infinispan-commons:jar:6.0.0.Final:compile
[INFO] |  |  |  +- org.jgroups:jgroups:jar:3.4.1.Final:compile
[INFO] |  |  |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.1.Final:compile
[INFO] |  |  |  +- org.jboss.marshalling:jboss-marshalling-river:jar:1.3.18.GA:compile
[INFO] |  |  |  +- org.jboss.marshalling:jboss-marshalling:jar:1.3.18.GA:compile
[INFO] |  |  |  \- org.jboss.logging:jboss-logging:jar:3.1.2.GA:compile
[INFO] |  |  \- displaytag:displaytag:jar:1.1:compile
[INFO] |  |     \- com.lowagie:itext:jar:1.3:compile
[INFO] |  +- thirdparty.secondrain:FrameworkCommonUtils:jar:jdk6:1.0:compile
[INFO] |  |  +- thirdparty.fr-search.frdoc:frdoc:jar:Provided:compile
[INFO] |  |  +- thirdparty.fastutil:fastutil:jar:5.1.5:compile
[INFO] |  |  +- thirdparty.wordnet:jaws-bin:jar:1.0:compile
[INFO] |  |  +- com.googlecode.matrix-toolkits-java:mtj:jar:0.9.14:compile
[INFO] |  |  |  \- com.googlecode.netlib-java:netlib-java:jar:0.9.3:compile
[INFO] |  |  |     \- net.sourceforge.f2j:arpack_combined_all:jar:0.1:compile
[INFO] |  |  +- org.mapdb:mapdb:jar:1.0.7:compile
[INFO] |  |  \- org.apache.httpcomponents:httpclient:jar:4.4:compile
[INFO] |  |     \- org.apache.httpcomponents:httpcore:jar:4.4:compile
[INFO] |  +- thirdparty.secondrain:FRSolrExtension:jar:jdk6:1.2:compile
[INFO] |  |  +- thirdparty.secondrain:FRSharedAPIServlet:jar:jdk6:1.0:compile
[INFO] |  |  +- net.sf.json-lib:json-lib:jar:jdk15:2.2.3:compile
[INFO] |  |  +- thirdparty.secondrain.solr:solr-solrj:jar:1.4.1:compile
[INFO] |  |  |  +- commons-fileupload:commons-fileupload:jar:1.2:compile
[INFO] |  |  |  +- org.apache.lucene:lucene-core:jar:2.9.3:compile
[INFO] |  |  |  +- org.codehaus.woodstox:wstx-asl:jar:3.2.7:compile
[INFO] |  |  |  \- org.apache.geronimo.specs:geronimo-stax-api_1.0_spec:jar:1.0.1:compile
[INFO] |  |  +- org.codehaus.jackson:jackson-core-lgpl:jar:1.5.3:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-mapper-lgpl:jar:1.5.3:compile
[INFO] |  +- thirdparty.secondrain:IndexEngine:jar:1.0.0:compile
[INFO] |  |  +- thirdparty.fr-search.IEngine.clientSDK.idsdk:fridsdk:jar:1.0:compile
[INFO] |  |  +- javax.servlet:javax.servlet-api:jar:3.0.1:compile
[INFO] |  |  +- thirdparty.tanukisoft:wrapper:jar:0:compile
[INFO] |  |  \- log4j:log4j:jar:1.2.16:compile
[INFO] |  +- thirdparty.sqljdbc:sqljdbc:jar:0:compile
[INFO] |  \- thirdparty.jxl:jxl:jar:0:compile
[INFO] +- org.springframework:spring-aop:jar:3.0.1.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  \- org.springframework:spring-asm:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:3.0.1.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- org.springframework:spring-expression:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework:spring-web:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.0.1.RELEASE:compile
[INFO] +- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] +- struts:struts:jar:1.2.4:compile
[INFO] +- thirdparty.jwnl:jwnl:jar:0:compile
[INFO] +- commons-net:commons-net:jar:1.2.1:compile
[INFO] |  \- oro:oro:jar:2.0.8:compile
[INFO] +- xmlunit:xmlunit:jar:1.0:compile
[INFO] +- backport-util-concurrent:backport-util-concurrent:jar:3.0:compile
[INFO] +- thirdparty.secondrain:frDB:jar:jdk6:1.0:compile
[INFO] |  \- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
[INFO] +- javax.servlet:jstl:jar:1.1.2:compile
[INFO] +- taglibs:standard:jar:1.1.2:compile
[INFO] +- thirdparty.secondrain:frDocMgr:jar:1.0:compile
[INFO] |  \- thirdparty.secondrain:frMessageService:jar:1.0.0:compile
[INFO] |     \- thirdparty.secondrain:Jamsel:jar:1.0.0:compile
[INFO] +- thirdparty.secondrain:EntityDomainModel:jar:jdk6:1.0:compile
[INFO] |  \- thirdparty.qazi:qazi:jar:Provided-1.2:compile
[INFO] +- org.powermock:powermock-core:jar:2.0.2:test
[INFO] |  +- org.powermock:powermock-reflect:jar:2.0.2:test
[INFO] |  |  \- org.objenesis:objenesis:jar:3.0.1:test
[INFO] |  +- org.javassist:javassist:jar:3.24.0-GA:test
[INFO] |  +- net.bytebuddy:byte-buddy:jar:1.9.3:test
[INFO] |  \- net.bytebuddy:byte-buddy-agent:jar:1.9.3:test
[INFO] +- org.powermock:powermock-module-junit4:jar:2.0.2:test
[INFO] |  +- org.powermock:powermock-module-junit4-common:jar:2.0.2:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.powermock:powermock-api-mockito2:jar:2.0.2:test
[INFO] |  +- org.powermock:powermock-api-support:jar:2.0.2:test
[INFO] |  \- org.mockito:mockito-core:jar:2.23.0:test
[INFO] \- junit:junit:jar:4.12:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.051 s
[INFO] Finished at: 2019-11-23T09:15:59-03:00
[INFO] Final Memory: 18M/309M
[INFO] ------------------------------------------------------------------------

mvn -v

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T13:41:47-03:00)
Maven home: C:\Users\Usuario\apache-maven-3.3.9\bin\..
Java version: 1.8.0_231, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_231\jre
Default locale: es_ES, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

java -version

java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

First of all, this:

javassist.CtClass.getDeclaredClasses()[Ljavassist/CtClass;

returns to a method in javassist.CtClass that has the signature

<modifier> javassist.CtClass getDeclaredClasses()

According to the published javadocs the "missing" method was added in javassist version 3.15.

So the immediate explanation is that the first JAR file for javassist that the JVM found on the runtime classpath was for an older version.

The Maven dependency tree shows that PowerMock 2.0.2 depends on

 org.javassist:javassist:jar:3.24.0-GA

But clearly there was another version of the JAR on the runtime classpath when you were running the tests. Mystery!

You then say that a clean project with the same dependencies doesn't have this problem. At least that makes sense. (To me.)


How to solve this?

There must a copy or copies of older (pre 3.15) javassist JAR files on your machine. Find it or them. For example, on Linux find $HOME -name javassist\\* -type f , and then eyeball the pathnames.

Next, try to find out what classpath is being used when you are running the tests.

  • If you are running them using a maven command, the -X (debug) option will show you a lot of extra information. According to Find classpath maven is using for running testng testcase , this includes the actual classpath.

  • If you are using a test runner in your IDE, check the IDE documentation.

Finally, analyse the classpath to find why there is an old javassist JAR on the path.

FWIW, my guess would be that you are running the tests in an IDE, and that somehow the IDE is adding the spurious JAR. This could have been a transitive dependency of an old (or upgraded) dependency that wasn't properly cleaned out by the IDE.

I know, it was long time ago, but I found this in a vietnamite forum ; this solution excludes the spurious JAR added by the IDE ... it was useful in my case

<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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