PowerMock抛出java.lang.ExceptionInInitializerError

时间:2017-12-25 12:43:04

标签: java mockito junit4 powermock

我正在使用Powermock + Mockito2 + JUnit4来测试一个类。

当我尝试运行ApiCreatorFacadeImplTest.java时,它不起作用并抛出java.lang.ExceptionInInitializerError。我不知道如何解决它。任何人都可以指出我犯了什么错误吗?

import static org.powermock.api.mockito.PowerMockito.mock;

@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpUtil.class})
public class ApiCreatorFacadeImplTest {

    private ServiceVersionFacade serviceVersionFacade;
    private ServiceFacade serviceFacade;

    private ApiCreatorFacadeImpl apiCreatorService;

    @Before
    public void setUp() {
        apiCreatorService = new ApiCreatorFacadeImpl();
        // mock ApiCreatorFacadeImpl fields
        serviceVersionFacade = mock(ServiceVersionFacade.class);
        serviceFacade = mock(ServiceFacade.class);
        // inject fields to apiCreatorService
        Whitebox.setInternalState(apiCreatorService, "serviceVersionFacade", serviceVersionFacade);
        Whitebox.setInternalState(apiCreatorService, "serviceFacade", serviceFacade);
    }

    @Test
    public void createHsfTypeApi() {
        // do nothing
    }

    @Test
    public void getApiTestParam() {
        // do nothing
    }
}

详细例外:

java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
    at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:308)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NullPointerException
    at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 38 more

2 个答案:

答案 0 :(得分:2)

可能的原因是Powermockito,Mockito的版本发生冲突。你能看一下Powermockito,mockito支持的版本吗?你可以看 this page

答案 1 :(得分:0)

我有同样的问题。这是由于Mockito库的版本不兼容。如果您已经在使用Mockito 1.x,则应使用powermock-api-mockito;在使用Mockito 2.x时,请使用powermock-api-mockito2。

P.S。例如,如果您使用的是Spring Boot和spring-boot-starter-test依赖项,则您的项目已包含Mockito依赖项,请检查Spring Boot Parent的BOM文件(带有依赖项版本的pom文件)以获取Mockito的版本。