我正在与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 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)
哦,是的,我正在使用Eclipse 3.6.2,JUnit 4.8.2和EasyMock 3.0.
编辑:似乎PowerMock可以处理从抽象类继承的模拟最终方法! http://code.google.com/p/powermock/wiki/MockFinal
解决方法:
我认为它与抽象类等无关.这是因为EasyMock无法模拟最终方法.从EasyMock documentation:
Final methods cannot be mocked. If called, their normal code will be executed
因此,您需要使您的方法不是最终的,或者使用其他一些不需要模拟它的测试方法.