问题描述
我在用 EasyMock 苦苦挣扎.我写了两个小类来说明我的问题:
I'm struggling with EasyMock. I've written two small classes to illustrate my problem:
public abstract class A {
private AtomicReference<Integer> id = new AtomicReference<Integer>(null);
public final int getId() {
return id.get();
}
public final boolean setId(int id) {
return this.id.compareAndSet(null, id);
}
}
public class B extends A {
}
然后我继续写一个测试方法如下:
Then I proceed to write a test method as follows:
public class EasyMockTester extends EasyMockSupport {
@Test
public void test() {
B b = EasyMock.createStrictMock(B.class);
EasyMock.expect(b.getId()).andReturn(100);
replayAll();
int id = b.getId();
System.out.println("The ID is: " + id);
verifyAll();
}
}
问题是我想让 EasyMock 简单地模拟 B 类的实例(我的实际类不是空的,而是向从抽象类继承的方法中添加了更多方法).相反,EasyMock 实际上以某种方式进入了 A 类的代码并开始抱怨 NullPointerException.如何让 EasyMock 模拟一个扩展抽象类的类?
The problem is that I want EasyMock to simply mock an instance of class B (my actual class isn't empty, but instead adds more methods to the methods inherited from the abstract class).Instead, EasyMock somehow actually goes into the code of class A and starts complaining about a NullPointerException.How do I make EasyMock mock a class that extends an abstract class?
当我运行此测试时,我得到以下故障跟踪:
When I run this test I get the following Failure trace:
java.lang.NullPointerException 在com.my.project.package.tests.A.getId(A.java:9)在com.my.project.package.tests.EasyMockTester.test(EasyMockTester.java:11)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在java.lang.reflect.Method.invoke(Method.java:597)在org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)在org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)在org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)在org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)在org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)在org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)在org.junit.runners.ParentRunner.run(ParentRunner.java:236)在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
哦,是的,我使用的是 Eclipse 3.6.2、JUnit 4.8.2 和 EasyMock 3.0.
Oh yeah, I'm using Eclipse 3.6.2, JUnit 4.8.2 and EasyMock 3.0.
似乎 PowerMock 可以处理从抽象类继承的模拟最终方法!http://code.google.com/p/powermock/wiki/MockFinal
Seems PowerMock can handle mocking final methods inherited from abstract classes! http://code.google.com/p/powermock/wiki/MockFinal
推荐答案
我觉得和抽象类等无关.这是因为 EasyMock 不能模拟 final
方法.来自 EasyMock 文档:
I think it's not related to abstract classes and so on. It's caused by the fact that EasyMock can't mock final
methods. From the EasyMock documentation:
最终方法不能被模拟.如果调用,它们的正常代码将被执行
因此,您需要使您的方法成为非最终方法,或者使用其他不需要模拟的方法进行测试.
So, you need to make your method non-final, or use some other approach to testing that doesn't require mocking it.
这篇关于如何使用 EasyMock 模拟从抽象类继承的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!