我正在试着做一个青少年测试
我使用的是:junit 4.10,
easymock 3.0,
powermock core 1.4.10(使用javassist 3.15.0-GA(,
powermock-module-unit4 1.4.10,
powermock api easymock 1.4.10
具有所有适当的依赖关系
我还尝试了powermock内核1.5.5(javassist 3.18.0-GA(
(请注意,只有一个依赖ognl(使用javassist3.7.ga(
测试类别:
@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClassToBeTested.class})
public class MyClassToBeTestedTest{
private MyClassToBeTested myClassToBeTested;
@Before
public void setUp() {
// Partial mock with powermock
myClassToBeTested = PowerMock.createPartialMock(MyClassToBeTested.class, "testMethod");
}
@Test // IF I COMMENT THE @Test ANNOTATION AND
public void testMethod() { // ALSO THE testMethod() THERE IS NO EXCEPTION
}
}
堆栈跟踪:
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0,08 sec
[junit]
[junit] Testcase: initializationError(com.myProject.MyClassToBeTested): Caused an ERROR
[junit] Failed to transform class with name com.myProject.MyClassToBeTested. Reason: 3
[junit] java.lang.IllegalStateException: Failed to transform class with name com.myProject.MyClassToBeTested. Reason: 3
[junit] at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:207)
[junit] at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:145)
[junit] at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:249)
[junit] at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java
3)
[junit] at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java
)
[junit] at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217)
[junit] at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59)
[junit] at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
[junit] at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:26)
[junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
[junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
[junit] Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
[junit] at javassist.bytecode.ByteArray.write16bit(ByteArray.java:40)
[junit] at javassist.bytecode.StackMapTable$Shifter.update(StackMapTable.java:744)
[junit] at javassist.bytecode.StackMapTable$Shifter.sameFrame(StackMapTable.java:720)
[junit] at javassist.bytecode.StackMapTable$Walker.stackMapFrames(StackMapTable.java:194)
[junit] at javassist.bytecode.StackMapTable$Walker.parse(StackMapTable.java:179)
[junit] at javassist.bytecode.StackMapTable$Shifter.doit(StackMapTable.java:714)
[junit] at javassist.bytecode.StackMapTable.shiftPc(StackMapTable.java:693)
[junit] at javassist.bytecode.CodeIterator.insertGap0(CodeIterator.java:676)
[junit] at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:636)
[junit] at javassist.bytecode.CodeIterator.insertGapCore(CodeIterator.java:467)
[junit] at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:413)
[junit] at javassist.expr.Expr.replace0(Expr.java:298)
[junit] at javassist.expr.FieldAccess.replace(FieldAccess.java:213)
[junit] at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:267)
[junit] at javassist.expr.ExprEditor.loopBody(ExprEditor.java:197)
[junit] at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
[junit] at javassist.CtClassType.instrument(CtClassType.java:1289)
[junit] at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:75)
[junit] at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:203)
谢谢你的建议。。。
我知道这是一个老问题,但在更改了一些其他依赖关系后,我最近也遇到了同样的错误。这似乎是javassist 3.18.2之前版本中的一个错误(我注意到您使用的是3.18.1(
我通过将Powermock升级到1.6.2版本并明确添加对Javassist 3.19.0-GA的依赖来修复它
此错误已在Javassist 3.18.2-GA中修复:https://issues.jboss.org/browse/JASSIST-223