gpt4 book ai didi

java - 使用静态枚举类模拟类时出现 "Failed to transform class with name"错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:52:30 25 4
gpt4 key购买 nike

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;
}
}

// [...]

}

最佳答案

我在我的一个测试中遇到了同样的问题,对我来说这是由 javassist 的版本冲突引起的。一些依赖项使用 javassist.javassist-3.4.GA,而 PowerMock 使用 org.javassist.javassist-3.18.1-GA(org. 是主要区别)。好像是eclipse先把前者放到classpath里的,所以用的是老版本。

我使用 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>

当然,您必须检查依赖项是否仍然适用于该版本。

关于java - 使用静态枚举类模拟类时出现 "Failed to transform class with name"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21603237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com