错误初始化 mockito,每次和任何测试用例

2022-09-02 10:39:26

几乎花了一整天的时间与此作斗争。无论我使用哪种测试/类/接口,我都会得到几乎相同的例外。下面是堆栈错误:

java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/MockMaker$TypeMockability;
    at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:26)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167)
    at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:58)
    at org.mockito.Mockito.mock(Mockito.java:1410)
    at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
    at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
    at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43)
    at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)
    at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:246)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    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:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    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:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

事实上,我甚至不明白为什么会涉及powermock库。当我试图嘲笑一些最终的课程时,它确实被编译了,但那是几个小时前的事了!

以下是我尝试使用的实际文件集:

public class ClassA {

    private String message;

    public String getMessage(){

        message="classA";
        return message;

    }
}

public class ClassB {

    private ClassA myClass;

    public ClassB(ClassA aClass){
        this.myClass=aClass;
    }

    public ClassA getClassA(){
        return myClass;
    }
}

和测试类:

package com.icidigital.services

import com.icidigital.users.ClassA
import com.icidigital.users.ClassB
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.runners.MockitoJUnitRunner
/**
 * Created by apil.tamang on 7/28/15.
 */
@RunWith(MockitoJUnitRunner.class)
class TestSimplestMock {

    ClassB classB;

    @Mock
    ClassA mockA;

    @Before
    public void setup(){


        classB=new ClassB(mockA);

        Mockito.when(classB.getClassA()).thenReturn(null);

    }

    @Test
    public void testA(){

        ClassA obj=classB.getClassA();

        assert(obj==null);

    }
}

答案 1

第三方插件可以通过Mockito的SPI注册自定义实现,这些实现创建与默认行为不同的类。作为指定行为的一部分,从 Mockito 2.0 开始,a 可以决定一个类是否是可模拟的,例如 PowerMock 中的类。为了集成这样的自定义行为,PowerMock 注册了一个自定义,然后由 Mockito 使用。MockMakerMockMakerfinalMockMaker

在 Mockito 2.0 中,必须扩展自定义的 API,但尚未调整 API 扩展。因此,这个新方法尚未由PowerMock实现,从而导致.MockMakerPowerMockMockMakerAbstractMethodError


答案 2

我遇到了同样的错误。对我来说,它有助于包括

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

而不是嘲笑和方法。 保持不变。mock()spy()@Mock


推荐