PowerMock的java.lang.VerifyError: Inconsistent stackmap frames at branch target异常分析

使用PowerMock进行单元测试时,要测试如下构造函数:

public FileSourcePc(Resource resource){
   ...
}

执行测试的过程中,每次运行到这里就会抛出如下异常:

java.lang.VerifyError: Inconsistent stackmap frames at branch target 126
Exception Details:
  Location:
    com/mydomain/commonlibrary/file/FileSourcePc.(Lcom/mydomain/commonlibrary/model/Resource;)V @126: aload_2
  Reason:
    Type uninitializedThis (current frame, locals[2]) is not assignable to 'com/mydomain/commonlibrary/file/FileSourcePc' (stack map, locals[2])
  Current Frame:
    bci: @112
    flags: { flagThisUninit }
    locals: { uninitializedThis, 'com/mydomain/commonlibrary/model/Resource', uninitializedThis, 'java/lang/String', 'java/lang/String', 'java/lang/String', integer, 'java/lang/String', 'java/lang/String', 'java/lang/String', top, 'java/lang/Object' }
    stack: { 'java/lang/Object', 'java/lang/Object' }
  Stackmap Frame:
    bci: @126
    flags: { flagThisUninit }
    locals: { uninitializedThis, 'com/mydomain/commonlibrary/model/Resource', 'com/mydomain/commonlibrary/file/FileSourcePc', 'java/lang/String', 'java/lang/String', 'java/lang/String', integer, 'java/lang/String', 'java/lang/String', 'java/lang/String' }
    stack: { }
  Bytecode:
    0000000: 2a2b b900 1701 0012 192b b900 1c01 0010
    0000010: 152b b900 1f01 002b b900 2201 0012 243a
    0000020: 093a 083a 0736 063a 053a 044e 4d12 26b8
    0000030: 002c 1007 bd00 2e59 032d 5359 0419 0453
    0000040: 5905 1905 5359 06bb 0030 5915 06b7 0033
    0000050: 5359 0719 0753 5908 1908 5359 1006 1909
    0000060: 5312 35b8 0039 b800 3f3a 0b19 0bb2 0043
    0000070: a500 0e2a 01c0 0045 b700 49a7 0016 2c2d
    0000080: 1904 1905 1506 1907 1908 1909 b700 4b01
    0000090: 572a b700 4fb1                         
  Stackmap Table:
    full_frame(@126,{UninitializedThis,Object[#15],Object[#16],Object[#18],Object[#18],Object[#18],Integer,Object[#18],Object[#18],Object[#18]},{})
    full_frame(@145,{UninitializedThis,Object[#15]},{})

	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
	at java.lang.Class.getDeclaredMethod(Class.java:2002)
	at org.mockito.cglib.proxy.Enhancer.getCallbacksSetter(Enhancer.java:631)
	at org.mockito.cglib.proxy.Enhancer.isEnhanced(Enhancer.java:605)
	at org.powermock.api.extension.proxyframework.ProxyFrameworkImpl.isProxy(ProxyFrameworkImpl.java:50)
	at org.powermock.reflect.internal.WhiteboxImpl.getUnmockedType(WhiteboxImpl.java:1661)
	at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock(DefaultConstructorExpectationSetup.java:92)
	at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withArguments(DefaultConstructorExpectationSetup.java:54)
	at com.mydomain.commonlibrary.file.FileTransferServiceTest.setup(FileTransferServiceTest.java:62)
	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 org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132)
	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
	at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
	at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


分析:

PowerMock中为支持对构造函数的测试,借助于Javassist实现对字节码的操作。但是从Java 6开始引入的Stack Map Frames特性与Javassist不兼容。在Java 6中该Stack Map Frames还是可选的。但是到了Java 7,该Stackmap Frames已经是默认使用的,所以不兼容问题导致了该异常。

解决:

解决方案有2个,一个是改变JVM的配置,一个是改变Javassist。Javassist目前的最新版本3.21.0-GA还不兼容该特性。

这里,我们为单元测试设置如下JVM 7参数,以禁用该Stack Map Frames特性。

-XX:-UseSplitVerifier


如果使用Maven的maven-surefire-plugin插件进行测试,还需要为该插件设置启动参数如下:


    
        
            org.apache.maven.plugins
            maven-surefire-plugin
            2.19.1
            
                -XX:-UseSplitVerifier
            
        
    

但是对于Java 8,由于上述JVM参数以及不存在,所以Stack Map Frames特性导致的字节码操作工具的兼容性问题无法避免。

 

参考文献:

http://blog.triona.de/development/jee/how-to-use-powermock-with-java-7.html

http://jboss-javassist.github.io/javassist/

http://chrononsystems.com/blog/java-7-design-flaw-leads-to-huge-backward-step-for-the-jvm

 

你可能感兴趣的:(PowerMock的java.lang.VerifyError: Inconsistent stackmap frames at branch target异常分析)