我几乎花了一整天的时间来对付这个问题。不管我使用什么样的测试/类/接口,我都得到了几乎相同的异常。下面是堆栈错误:
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库。当我试图模拟一些final类时,它确实被编译了,但那是几个小时前的事了!
下面是我尝试使用的一组实际文件:
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);
}
}
6条答案
按热度按时间bfnvny8b1#
第三方插件可以注册定制的
MockMaker
实现,通过Mockito的SPI创建不同于默认行为的类。作为指定行为的一部分,从Mockito 2.0开始,MockMaker
可以决定一个类是否是可模拟的,比如PowerMock中的final
类。为了集成这样的定制行为,PowerMock注册一个自定义的MockMaker
,然后由Mockito使用。在Mockito 2.0中,自定义
MockMaker
的API必须进行扩展,但是PowerMock
还没有适应API扩展。因此,PowerMockMockMaker
还没有实现这个新方法,这导致了AbstractMethodError
。zxlwwiss2#
我遇到了同样的错误。对我来说,它有助于包括
而不是Mockitos的
mock()
和spy()
方法。@Mock
保持不变。zzoitvuj3#
正确答案由 Raphael Winterhalter 给出。但目前PowerMockito团队正在努力引入对Mockito2+的支持。目前最新的稳定版本是RC4
它编译但仍有很多问题
这里是关于Mockito 2支持https://github.com/powermock/powermock/wiki/Mockito-2-(Maven)的Wiki页面
xv8emn3q4#
不确定这是否是你要做的。但是如果你试图在调用getClassA时返回null,那么你应该模拟ClassB类型的对象!(因为getClassA是该类的一个方法)。
lzfw57am5#
我在使用mockito-core:jar:2.18.3和powermock-api-mockito:1.6.3时也遇到了同样的问题。
bogh5gae6#
存在版本不兼容问题:在pom中的以下版本中,一切都正常: