我侦察一个对象来测试一个方法
List<HighWay> mockedList = mock(List.class);
request = Mockito.spy(new HighWayRequest());
Mockito.doReturn(mockedList).when(request).getHighWays();
request.updateNewHighWay(repository);
updateNewHighWay方法调用我要返回列表的getHighWays方法, 而真正的请求方法就是那样
public void updateNewHighWay(HighWayRepository repository) {
List<HighWay> total = getHighWays();
List<HighWay> willSave = new ArrayList<HighWay>();
for (HighWay highWay : total) {
if (!repository.exists(highWay.getId())) {
willSave.add(highWay);
repository.save(highWay);
}
}
while (total.size() == willSave.size()) {
total = getHighWays();
willSave = new ArrayList<HighWay>();
for (HighWay highWay : total) {
if (!repository.exists(highWay.getId())) {
willSave.add(highWay);
repository.save(highWay);
}
}
}
}
public List<HighWay> getHighWays() {
// TODO Auto-generated method stub
return null;
}
但实际上它会引发一场愚蠢的行为。
java.lang.NullPointerException at chinahighway.HighWayRequest.updateNewHighWay(HighWayRequest.java:206) at chinahighway.HttpTest.testpageinsert(HttpTest.java:269)at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73) 在 org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) 在 org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) 在org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
为什么doReturn不起作用?如何让getHighWays方法返回我想要的列表?
请帮助我非常感谢!