调用PowerMockito.mockStatic时的AbstractMethodError

时间:2015-05-22 17:37:36

标签: java mockito powermockito

我试图使用PowerMockito模拟静态调用。但是,当我尝试这样做时,PowerMockito.mockStatic调用抛出一个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)

由于这是我在单元测试方法中做的第一件事,但它实际上还没有与我自己的代码进行交互。

我尝试模拟的类不在Spring安全库中: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
    }

}

我有以下Maven依赖项:

    <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>

(EasyMock的东西并没有在这个测试中使用,但是为了完整性我将它们包含在内,以防出现某种冲突。我已尝试过mockito-all和mockito-core,没有可观察到的行为差异。删除直接mockito依赖(mockito-all或mockit-core)也没有区别。)

我做错了什么?

0 个答案:

没有答案