AbstractMethodError when calling PowerMockito.mockStatic

时间秒杀一切 提交于 2019-12-24 03:48:07

问题


I'm trying to use PowerMockito to mock a static call. However, when I try to do so, the PowerMockito.mockStatic call throws an AbstractMethodError:

java.lang.AbstractMethodError: org.mockito.configuration.MockitoConfiguration.enableClassCache()Z
    at org.mockito.internal.configuration.GlobalConfiguration.enableClassCache(GlobalConfiguration.java:63)
    at org.mockito.internal.creation.instance.ObjenesisInstantiator.<init>(ObjenesisInstantiator.java:11)
    at org.mockito.internal.creation.instance.InstantiatorProvider.<clinit>(InstantiatorProvider.java:7)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)

Since it's the first thing I do in my unit test method, it's not actually interacting with any of my own code yet.

The class I'm trying to mock is out of the Spring security library: SecurityContextHolder.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ SecurityContextHolder.class })
@PowerMockIgnore({ "org.slf4j.*", "javax.management.*", "org.apache.log4j.*"})
public class AuthorizationSuccessHandlerTest {

    @Test
    public void testAuthenticationSuccess() throws Exception {
        // this first line throws the exception:
        PowerMockito.mockStatic(SecurityContextHolder.class); 

        SecurityContext mockContext = Mockito.mock(SecurityContext.class);
        Mockito.when(SecurityContextHolder.getContext()).thenReturn(mockContext);
        // .. other stuff to follow that we never get to
    }

}

I have the following Maven dependencies:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-easymock</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.0</version>
        <scope>test</scope>
    </dependency>

(The EasyMock stuff isn't used in this test, but I include them for completeness in case there's some kind of conflict. I've tried both mockito-all and mockito-core, with no observable difference in behavior. Removing the direct mockito dependency (mockito-all or mockit-core) makes no difference either.)

What am I doing wrong?

来源:https://stackoverflow.com/questions/30402750/abstractmethoderror-when-calling-powermockito-mockstatic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!