花了一整天时间来打这个。我使用什么样的测试/类/接口并不重要,我得到了几乎相同的异常。这是stackerror:
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);
}
}
答案 0 :(得分:9)
第三方插件可以注册自定义MockMaker
实现,这些实现通过Mockito的SPI创建与默认行为不同的类。作为指定行为的一部分,从Mockito 2.0开始,MockMaker
可以决定一个类是否可模拟,例如PowerMock中的final
类。为了集成这样的自定义行为,PowerMock会注册一个随后由Mockito使用的自定义MockMaker
。
在Mockito 2.0中,必须扩展自定义MockMaker
的API,但PowerMock
尚未调整API扩展。因此,PowerMock MockMaker
尚未实现此新方法,导致AbstractMethodError
。
答案 1 :(得分:4)
我遇到了同样的错误。对我而言,它有助于包括
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
而不是Mockitos mock()
和spy()
方法。 @Mock
保持不变。
答案 2 :(得分:1)
Not sure if that is what you are trying to do. But if you are trying to return null whenever getClassA is called, then you should mock object of type ClassB! (since getClassA is a method of that class).
答案 3 :(得分:1)
Raphael Winterhalter 给出了正确的答案。但目前 PowerMockito 团队正致力于为Mockito2 +提供支持 目前最新的稳定版本是RC4
testCompile 'org.powermock:powermock-api-mockito2:1.7.0RC4'
它编译但仍然存在很多问题
这是关于Mockito 2支持的Wiki页面 https://github.com/powermock/powermock/wiki/Mockito-2-(Maven)
答案 4 :(得分:0)
在使用嘲笑内核:jar:2.18.3和powermock-api-mockito:1.6.3时,我遇到了同样的问题。 当我降级到嘲笑核心:jar:2.0.5-beta时,它对我有用。