我已经为这个错误苦苦挣扎了两天。我正在使用Intellij进行春季Maven项目。
我尝试了在SO中找到的每个问题,却一无所获。我尝试了很多版本,但一无所获。
我完全被以下异常困住了:
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)
这是我的代码:
测试
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() {}
}
如果我删除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依赖项
<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>
依赖树
[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)
答案 0 :(得分:1)
首先,这是
javassist.CtClass.getDeclaredClasses()[Ljavassist/CtClass;
返回javassist.CtClass
中具有签名的方法
<modifier> javassist.CtClass getDeclaredClasses()
根据published javadocs,在javassist版本3.15中添加了“ missing”方法。
所以直接的解释是,javassist的 first JAR文件是在运行时类路径上找到的JVM用于较旧的版本。
Maven依赖关系树显示PowerMock 2.0.2依赖
org.javassist:javassist:jar:3.24.0-GA
但是,显然,在运行测试时,运行时类路径上还有另一个JAR版本。神秘!
然后,您说具有相同依赖项的干净项目不会出现此问题。至少那有意义。 (对我来说。)
如何解决这个问题?
您的计算机上必须有一个或多个旧的(3.15之前的版本)javassist JAR文件的副本。找到它或他们。例如,在Linux find $HOME -name javassist\* -type f
上,然后注意路径名。
接下来,尝试找出运行测试时正在使用的类路径。
如果使用maven命令运行它们,则-X
(调试)选项将为您显示很多其他信息。根据{{3}},其中包括实际的类路径。
如果您在IDE中使用测试运行程序,请查看IDE文档。
最后,分析类路径以找出为什么路径上存在旧的Javassist JAR。
FWIW,我的猜测是您正在IDE中运行测试,并且IDE以某种方式添加了虚假的JAR。这可能是旧的(或升级的)依赖项的可传递依赖项,而该依赖项没有被IDE正确清除。