2016-09-15 308 views
-2
public interface ABC { 
     public boolean removeUser(String userId) throws OTPServiceException, RemoteException; 
} 

ABC abc= mock(ABC.class); 
doNothing().when(abc).removeUser(anyString()); 

我試過這樣。我得到了以下例外。Mockito拋出異常

org.mockito.exceptions.base.MockitoException: 
Only void methods can doNothing()! 
Example of correct use of doNothing(): 
    doNothing(). 
    doThrow(new RuntimeException()) 
    .when(mock).someVoidMethod(); 
Above means: 
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called 
+0

你擔心什麼? – talex

+0

你能解釋一下你的問題嗎? –

+0

他問你你的問題是什麼。信息非常清楚「只有無效的方法才能做到沒有()!」所以你谷歌消息並解決你的問題?! – GhostCat

回答

4

你方法返回一個布爾值,所以你應該模擬一個布爾響應。

你應該有這樣的事情:

when(abc.removeUser(anyString())).thenReturn(true); 

您可以檢查Usages of doThrow() doAnswer() doNothing() and doReturn() in mockito獲得更詳細的,簡單的解釋。

+0

編輯回覆以使其正確。 我正在調用thenReturn removeUser(anyString()),它應該在when(...)方法的結果上調用。 – djointster

1

你可以因爲你需要對非void方法不doNothing要麼返回的東西或拋出異常。

when(abc.removeUser(anyString())).thenReturn(true); 

when(abc.removeUser(anyString())).thenThrow(RuntimeException.class);