wujun / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Failed to transform class with name com.test.NewTest #439

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm getting a "Failed to transform class with name com.test.NewTest. Reason: 3" 
error when mocking a static method. 

My test is this:

@Test
@PrepareForTest(Counter.class)
public class NewTest {
    public void test_add() {
        PowerMockito.mockStatic(Counter.class);
        PowerMockito.when(Counter.add(1, 1)).thenReturn(2);
        int result = Counter.add(1, 1);
        Assert.assertEquals(result, 2);
    }

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }
}

And Counter class:

public class Counter {
    public static int add(int a, int b) {
        return a + b;
    }
}

The stack trace when exception is thrown is:

org.testng.TestNGException: 
An error occurred while instantiating class com.test.NewTest: Failed to 
transform class with name com.test.NewTest. Reason: 3
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:398)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:111)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:413)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:142)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1278)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1265)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1119)
    at org.testng.TestNG.run(TestNG.java:1036)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.IllegalStateException: Failed to transform class with name 
com.test.NewTest. Reason: 3
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:207)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:145)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.powermock.modules.testng.PowerMockObjectFactory.createTestClass(PowerMockObjectFactory.java:106)
    at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:79)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    ... 20 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
    at javassist.bytecode.ByteArray.write16bit(ByteArray.java:40)
    at javassist.bytecode.StackMapTable$Shifter.update(StackMapTable.java:744)
    at javassist.bytecode.StackMapTable$Shifter.sameFrame(StackMapTable.java:720)
    at javassist.bytecode.StackMapTable$Walker.stackMapFrames(StackMapTable.java:194)
    at javassist.bytecode.StackMapTable$Walker.parse(StackMapTable.java:179)
    at javassist.bytecode.StackMapTable$Shifter.doit(StackMapTable.java:714)
    at javassist.bytecode.StackMapTable.shiftPc(StackMapTable.java:693)
    at javassist.bytecode.CodeIterator.insertGap0(CodeIterator.java:676)
    at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:636)
    at javassist.bytecode.CodeIterator.insertGapCore(CodeIterator.java:467)
    at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:413)
    at javassist.expr.Expr.replace0(Expr.java:298)
    at javassist.expr.FieldAccess.replace(FieldAccess.java:213)
    at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:276)
    at javassist.expr.ExprEditor.loopBody(ExprEditor.java:197)
    at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
    at javassist.CtClassType.instrument(CtClassType.java:1289)
    at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:75)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:203)
    ... 28 more

I got the exception when adding this test to an existing project. However, it 
works fine if I create a new project with just these two classes. Therefore 
this could be an envrionement issue. 

I'm using these verions:
testng 5.14.6, mockito 1.8.5, powermock 1.4.12

The operating system is Windows 7.

How do I troubleshoot this? Are there loggings I can turn on?

Let me know if you need more information.

Thanks!

Original issue reported on code.google.com by deliang...@dianping.com on 12 Apr 2013 at 2:51