Powermock-无法转换具有名称的类... ArrayIndexOutOfBoundsException:3

时间:2014-08-04 15:25:38

标签: java powermock


我试图进行junit测试。

我正在使用:junit 4.10,
            easymock 3.0,
            powermock-core 1.4.10(使用javassist 3.15.0-GA),
            powermock-module-junit4 1.4.10,
            powermock-api-easymock 1.4.10
 具有所有适当的依赖性。
 我也试过powermock-core 1.5.5(javassist 3.18.0-GA)

(请注意,只有一个依赖项ognl(使用javassist 3.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)

感谢您提出任何建议......

1 个答案:

答案 0 :(得分:1)

我知道这是一个老问题,但在更改其他依赖项后,我最近得到了完全相同的错误。它似乎是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