java.lang.IllegalStateException: Failed to transform class with name com.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX$ReadSets. Reason: null
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:219)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:390)
at sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:371)
at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:673)
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:480)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:306)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:241)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3089)
at java.lang.Class.getAnnotations(Class.java:3069)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.classAnnotations(PowerMockJUnit44RunnerDelegateImpl.java:163)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.getDescription(PowerMockJUnit44RunnerDelegateImpl.java:155)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getDescription(JUnit4TestSuiteChunkerImpl.java:171)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.getDescription(AbstractCommonPowerMockRunner.java:47)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.sendTree(JUnit4TestClassReference.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.sendTrees(RemoteTestRunner.java:476)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:110)
at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:96)
at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:704)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:681)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:156)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
at javassist.compiler.Javac$1.setReturnType(Javac.java:449)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:146)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
at javassist.compiler.TypeChecker.atVariableAssign(TypeChecker.java:248)
at javassist.compiler.TypeChecker.atAssignExpr(TypeChecker.java:217)
at javassist.compiler.ast.AssignExpr.accept(AssignExpr.java:38)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:235)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:323)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:344)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atIfStmnt(CodeGen.java:384)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:348)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:344)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.Javac.compileStmnt(Javac.java:558)
at javassist.expr.MethodCall.replace(MethodCall.java:233)
at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:299)
at javassist.expr.ExprEditor.loopBody(ExprEditor.java:178)
at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
at javassist.CtClassType.instrument(CtClassType.java:1224)
at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:75)
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:215)
... 28 more
这个类包含一个静态方法getInstance(我想模拟),提供一个单例对象和几个声明为public static的Enum类(在异常中可以看到其中一个名为ReadSets)。以下是该课程的样子。
我试图使用PowerMockito.mock和Powermockito.MockStatic在我的测试方法中模拟这个类,它会抛出异常。
有关如何修复的任何线索?
public class XXX extends YYY {
private volatile static XXX s_instance;
public static XXX getInstance() {
if (s_instance == null)
synchronized (XXX.class) {
if (s_instance == null)
s_instance = new XXX();
}
return s_instance;
}
public static enum ReadSets {
ANY(1), ALL(2);
int val;
public int getVal() {
return val;
}
private ReadSets(int v) {
val = v;
}
}
public static enum UpdateSets {
ANY(1), ALL(2);
int val;
private UpdateSets(int v) {
val = v;
}
}
// [...]
}
答案 0 :(得分:0)
我在我的一个测试中遇到了同样的问题,对我而言,这是因为javassist的冲突版本。 一些依赖使用javassist.javassist-3.4.GA,而PowerMock使用org.javassist.javassist-3.18.1-GA( org。是主要区别)。 似乎eclipse首先将前者放入类路径中,因此使用旧版本。
我使用maven,所以我可以通过在依赖项中为javassist.javassist创建一个排除规则来解决冲突,该依赖项会直接依赖于更新的jassist版本。不是最好的解决方案,但我不知道在maven中处理这些人工制品的更好方法。
<dependency>
<groupId>some</groupId>
<artifactId>dependency</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
当然,您必须检查依赖项是否仍适用于该版本。
答案 1 :(得分:0)
我遇到了同样的问题并提到了答案。事实证明鲍里斯是正确的。 PowerMock引入了版本为3.18.2-GA的javassist,导致一些事情行为不端。在我的情况下,某些单元测试仅在我尝试构建整个项目时才开始失败。
我添加了以下依赖
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.4.GA</version>
<scope>test</scope>
</dependency>
这样Powermock对自己的javassist(org.javassist)很满意,测试对新的javassist 3.4.GA很满意。
但是,我不确定是什么原因导致测试失败,只有当我构建整个项目时才会失败。