【发布时间】:2016-08-19 09:56:34
【问题描述】:
我有以下方法在我的Java 服务类中进行测试:
public void findPersonToDelete(String id){
//repository method then called
personRepository.findPersonAndDelete(id);
}
问题在于 personRepository 调用了其他代码,该代码抛出了Null Pointer。
我尝试使用以下行来阻止 personRepository 调用其他方法:
Mockito.doNothing().when(personRepository).findPersonAndDelete(id);
但是错误仍然存在?我该如何解决这个问题?
【问题讨论】:
-
如果你没有用
id的任何值调用它,它将正常执行该方法。 -
personRepository 是模拟的吗?
-
尝试
anyInt()而不是特定值。另外PersonRepository需要是mock或spy。 -
@Heisenberg 是的,它是一个模拟
标签: java unit-testing nullpointerexception mockito method-call