Mockito在创建模拟对象时抛出NullPointer

2022-09-04 04:08:24

我有一个集成测试,其中一些设置是使用Guice完成的。我正在使用Mockito来模拟一些依赖项。到目前为止,这对我来说一直很好。我需要使用PowerMock来实现其他一些依赖关系。现在,Mockito正在抛出一个加载自己的类:NullPointerException

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中已知错误的信息。有没有人对如何解决这个问题有建议?


答案 1

检查此问题

当在即时时间创建模拟时,似乎会发生这种情况。

如果我以@Before方法设置模拟,问题就会消失。


答案 2

推荐