【问题标题】:Powermock-Failed to transform class with name...ArrayIndexOutOfBoundsException: 3Powermock-无法转换具有名称的类...ArrayIndexOutOfBoundsException:3
【发布时间】:2014-08-04 15:25:38
【问题描述】:


我正在尝试运行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)

感谢您的任何建议...

【问题讨论】:

  • 你自己解决了吗?
  • 不...我试过了,但我没能找到问题所在。我终于换成了 Jmockit。

标签: java powermock


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多