创建一个模拟对象时抛出的Mockito空指针(Mockito throws NullPointer

2019-08-18 01:14发布

我有一个集成测试,其中一些设立使用吉斯完成。 我使用到的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已知的bug什么。 有没有人对如何解决这一建议?

Answer 1:

检查这个问题

这似乎当instantation时创建一个模拟的情况发生。

如果我在一个@Before方法设置我嘲笑这个问题消失。



文章来源: Mockito throws NullPointer when creating a mock object