2013-03-26 72 views
7

我有一個集成測試,其中一些設置是使用Guice完成的。我正在使用Mockito來模擬一些依賴關係。直到現在,這對我來說工作得很好。我需要使用PowerMock來處理其他依賴項。現在是的Mockito扔NullPointerException同時加載了自己的一類:Mockito在創建模擬對象時拋出NullPointer

java.lang.ExceptionInInitializerError 
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17) 
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30) 
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19) 
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18) 
    at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145) 
    at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110) 
    at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106) 
    at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59) 
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21) 
    at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40) 
    at org.mockito.Mockito.<clinit>(Mockito.java:932) 
    <snip my test code and some Guice> 
    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:601) 
    at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:56) 
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:43) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120) 
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101) 
    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:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

Caused by: java.lang.NullPointerException 
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21) 
    ... 39 more 

的(下調)代碼:

@PrepareForTest(MyModule.class) 
@RunWith(PowerMockRunner.class) 
public class MyTest { 
    @BeforeClass 
    public static void setUp() { 
     Injector injector = Guice.createInjector(...); 
     MyModule module = PowerMockito.mock(MyModule.class); 
     injector = injector.createChildInjector(...); // Fails in another Mockito.mock() call before it gets to use the mocked Module. 
    } 
} 

我找不到這事是在一個的Mockito已知的bug。有沒有人有如何解決這個問題的建議?

回答

6

檢查this issue

這似乎當instantation時創建一個模擬的情況發生。

如果我在@Before方法中設置我的模擬,問題就會消失。

+0

我將一些代碼從'@ BeforeClass'移到'@ Before',所以稍後調用它。原來的問題然後消失,但我現在遇到其他錯誤,其中包括這個:http://code.google.com/p/powermock/issues/detail?id=277 – Jorn 2013-03-26 14:08:27

+0

沒有看到這個。但根據鏈接,這應該解決 - '[@PowerMockIgnore(「javax.management。*」)]解決它' – rajesh 2013-03-27 04:19:16