How do I mock a method inherited from an abstract class with 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();
}
}
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.BlockJUnit4Class Runner.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
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:
Final methods cannot be mocked. If called, their normal code will be executed
So, you need to make your method non-final, or use some other approach to testing that doesn't require mocking it.
链接地址: http://www.djcxy.com/p/90362.html