Willam2004 / powermock

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

PowerMockTestCase subclass test crashes #545

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
While writing a test with powermock 1.6.2 and TestNG 6.8.21 when a test of 
class that subclasses PowerMockTestCase there is a consistent crash.

Code under test:
package p;

public class Foo {
    public static void foo() throws Exception {
        throw new Exception("huh?");
    }
}

Test class:
package p;

import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import p.Foo;

import static org.powermock.api.mockito.PowerMockito.mockStatic;

@PrepareForTest({p.Foo.class})
public class FooTest extends PowerMockTestCase {

    @BeforeMethod
    public void setup() {
        mockStatic(FooTest.class);
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test() throws Exception {
        PowerMockito.doNothing().when(FooTest.class,"foo");
        p.Foo.foo();

        Assert.assertEquals(1, 1);
    }
}

The error:
org.testng.TestNGException: 
An error occurred while instantiating class p.FooTest: Bad <init> method call 
from inside of a branch
Exception Details:
  Location:
    p/FooTest.<init>()V @32: invokespecial
  Reason:
    Error exists in the bytecode
  Bytecode:
    0000000: 2a4c 1210 b800 1603 bd00 0d12 17b8 001b
    0000010: b800 214e 2db2 0025 a500 0e2a 01c0 0027
    0000020: b700 2aa7 0009 2bb7 002c 0157 b1       
  Stackmap Table:
    full_frame(@38,{UninitializedThis,UninitializedThis,Top,Object[#13]},{})
    full_frame(@44,{Object[#2],Object[#2],Top,Object[#13]},{})

    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:391)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:293)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:115)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:200)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    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:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:575)
    at org.testng.SuiteRunner.init(SuiteRunner.java:159)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:113)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    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)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
Caused by: java.lang.VerifyError: Bad <init> method call from inside of a branch
Exception Details:
  Location:
    p/FooTest.<init>()V @32: invokespecial
  Reason:
    Error exists in the bytecode
  Bytecode:
    0000000: 2a4c 1210 b800 1603 bd00 0d12 17b8 001b
    0000010: b800 214e 2db2 0025 a500 0e2a 01c0 0027
    0000020: b700 2aa7 0009 2bb7 002c 0157 b1       
  Stackmap Table:
    full_frame(@38,{UninitializedThis,UninitializedThis,Top,Object[#13]},{})
    full_frame(@44,{Object[#2],Object[#2],Top,Object[#13]},{})

    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
    at java.lang.Class.getConstructor0(Class.java:2885)
    at java.lang.Class.getConstructor(Class.java:1723)
    at org.powermock.modules.testng.internal.PowerMockClassloaderObjectFactory.newInstance(PowerMockClassloaderObjectFactory.java:86)
    at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:42)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:380)
    ... 31 more

Process finished with exit code 0

PS, converting to JUnit solves the issue, but we prefer TestNG.

Original issue reported on code.google.com by ashern...@gmail.com on 7 Apr 2015 at 12:21