2016-03-07 218 views
4

我想使用TestNG的+ powermock +春天來運行測試代碼,我的罐子版本是:java.lang.NoClassDefFoundError:無法初始化類org.mockito.internal.util.MockUtil

powermock = 1.6.2 
powermock-module-testng = 1.6.2 
mockito = 1.10.19 
testng = 6.8.7 
spring = 4.1.1.RELEASE 
JDK = 1.8.0 

和我測試代碼如下,並且不延伸AutoBaseTest,可以運行正常,AutoBaseTest包含 @ContextConfiguration(位置= { 「/測試/資源/的applicationContext-的test.xml」})

@PrepareForTest({test.class}) 


    public class mockTest extends AutoBaseTest { 

    @ObjectFactory 
    public IObjectFactory getObjectFactory() { 

     return new PowerMockObjectFactory(); 
    } 


    @Test 
    public final void test() throws Exception { 
     ...... 
    } 
    } 

錯誤堆棧:

`FAILED CONFIGURATION: @BeforeClass springTestContextBeforeTestClass 
java.lang.ExceptionInInitializerError 
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.(ConditionalStackTraceFilter.java:17) 
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41) 
at org.mockito.exceptions.base.MockitoException.(MockitoException.java:30) 
at org.mockito.exceptions.misusing.MockitoConfigurationException.(MockitoConfigurationException.java:18) 
at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66) 
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24) 
at org.mockito.internal.configuration.plugins.PluginRegistry.(PluginRegistry.java:12) 
at org.mockito.internal.configuration.plugins.Plugins.(Plugins.java:11) 
at org.mockito.internal.util.MockUtil.(MockUtil.java:24) 
at org.mockito.internal.configuration.injection.scanner.MockScanner.(MockScanner.java:22) 
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96) 
at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:35) 
at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:60) 
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873) 
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:663) 
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415) 
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:75) 
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:47) 
at test.java.com.sosoPay.servicetest.mockTest_$$jvstb5f_0.springTestContextBeforeTestClass(mockTest$$_jvstb5f_0.java) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564) 
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213) 
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138) 
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107) 
at org.testng.TestRunner.privateRun(TestRunner.java:767) 
at org.testng.TestRunner.run(TestRunner.java:617) 
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335) 
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330) 
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 
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) 
Caused by: java.lang.NullPointerException 
at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17) 
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.(StackTraceFilter.java:21) 
... 48 more 

FAILED CONFIGURATION: @AfterMethod springTestContextAfterTestMethod(public final void test.java.com.sosoPay.servicetest.mockTest.test() throws java.lang.Exception) 
java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.util.MockUtil 
at org.mockito.internal.configuration.injection.scanner.MockScanner.(MockScanner.java:22) 
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96) 
at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:35) 
at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:60) 
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873) 
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:663) 
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415) 
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:75) 
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:47) 
at test.java.com.sosoPay.servicetest.mockTest_$$jvstb5f_0.springTestContextAfterTestMethod(mockTest$$_jvstb5f_0.java) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564) 
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213) 
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786) 
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
at org.testng.TestRunner.privateRun(TestRunner.java:767) 
at org.testng.TestRunner.run(TestRunner.java:617) 
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335) 
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330) 
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 
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) 

FAILED CONFIGURATION: @AfterClass springTestContextAfterTestClass 
java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.util.MockUtil 
at org.mockito.internal.configuration.injection.scanner.MockScanner.(MockScanner.java:22)` 
+0

java.lang.NoClassDefFoundError:無法初始化類org.mockito.internal.util.MockUtil ==>類似於你的lokks缺少一些依賴關係。 –

+0

的Mockito-全1.10.19.jar \t \t的Mockito核心 - 1.10.19.jar \t \t powermock-API的Mockito-1.6.2.jar \t \t powermock核-1.6.2.jar \t \t powermock-API支持-1.6.2.jar \t \t powermock - 反射 - 1.6.2.jar \t \t powermock模塊-junit4-1.6.2.jar \t \t powermock模塊-junit4-共1.6 .2.jar \t \t powermock-module-tes tng-1.6.2.jar \t \t powermock-module-testng-common-1.6.2.jar – chen

+0

確保你遵循了使用TestNG和Powermock的建議,尤其是關於'PowerMockObjectFactory'的解釋[這裏]( https://github.com/jayway/powermock/wiki/TestNG_usage)。一旦使用了正確的工廠,您可能會發現您的問題更容易診斷。 –

回答

4

這是一個defect in Mockito帶有加載插件,當上下文類加載器不同於用於加載PowerMock和Mockito。

它可以被固定的Mockito 2,但因爲它在評論

Not sure we have the build infrastructure to backport the fix on 1.x

有關的Mockito 1中提到,解決無法送達,所以我在PowerMock固定和修復將包括在明年PowerMock版本( PowerMock 1.6.5)。

相關問題