如何使用 EasyMock 模拟从抽象类继承的方法?
我正在与 EasyMock 作斗争。我编写了两个小类来说明我的问题:
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 {
}
然后我继续编写一个测试方法,如下所示:
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 模拟一个扩展抽象类的类?
当我运行此测试时,我得到以下失败跟踪:
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(Native 方法)在 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。
编辑:似乎 PowerMock 可以处理从抽象类继承的模拟最终方法! http://code.google.com/p/powermock/wiki/MockFinal
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();
}
}
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 at
com.my.project.package.tests.A.getId(A.java:9)
at
com.my.project.package.tests.EasyMockTester.test(EasyMockTester.java:11)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
java.lang.reflect.Method.invoke(Method.java:597)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at
org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at
org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Oh yeah, I'm using Eclipse 3.6.2, JUnit 4.8.2 and EasyMock 3.0.
EDIT: Seems PowerMock can handle mocking final methods inherited from abstract classes! http://code.google.com/p/powermock/wiki/MockFinal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这与抽象类等无关。这是因为 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.