2016-08-19 72 views
0

我有以下方法在我Java服務類測試:如何停止在Mockito的另一個類中調用方法?

public void findPersonToDelete(String id){ 

    //repository method then called 
    personRepository.findPersonAndDelete(id); 


} 

的問題是,personRepository調用它拋出一個Null Pointer其他代碼。

我嘗試使用下面的行從調用其他方法停止personRepository:

Mockito.doNothing().when(personRepository).findPersonAndDelete(id); 

但錯誤依然堅持?我怎樣才能解決這個問題?

+1

如果你不與任何價值'id',它會正常執行的方法調用它。 – byxor

+0

personRepository是一個模擬嗎? – Heisenberg

+2

嘗試'anyInt()'而不是特定的值。進一步的'PersonRepository'需要是'mock'或'spy'。 –

回答

1

如果你有一個設置你的PersonRepository你可以嘲笑類,並將其設置在你的服務,這樣的嘲笑類將被調用,您可以只檢查它是否被調用。 所以這樣的東西:

PersonRepository repo = Mockito.mock(PersonRepository.class); 
service.setPersonRepository(repo); 
service.findPersonToDelte(1); 

然後檢查你想測試。

相關問題