我有以下方法可以在Java
服务类中进行测试:
public void findPersonToDelete(String id){
//repository method then called
personRepository.findPersonAndDelete(id);
}
问题是personRepository调用其他引发
Null Pointer
的代码。我试图使用以下行来阻止personRepository调用其他方法:
Mockito.doNothing().when(personRepository).findPersonAndDelete(id);
但是错误仍然存在吗?我怎样才能解决这个问题?
最佳答案
如果您的PersonRepository有一个setter,则可以模拟该类并在您的服务中对其进行设置,这样将调用模拟的类,并且您只能检查它是否被调用。
所以像这样:
PersonRepository repo = Mockito.mock(PersonRepository.class);
service.setPersonRepository(repo);
service.findPersonToDelte(1);
然后检查您要测试的内容。
关于java - 如何停止Mockito中另一个类中的方法调用方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39036101/