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

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

私はこれを試しました。私は例外以下になった。非void方法について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

彼はあなたの質問が何であるか尋ねています。このメッセージは非常に明確です。「voidメソッドのみが何もできません!」だからあなたはGoogleにメッセージを送り、あなたの問題を解決しますか? – GhostCat

答えて

4

あなたのメソッドはブール値を返すので、ブール型応答をモックする必要があります。あなたはより詳細かつシンプルな、について説明するためにUsages of doThrow() doAnswer() doNothing() and doReturn() in mockitoを確認することができます

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

あなたはこのようなものを持っている必要があります。

+0

正しいように応答を編集しました。 私はthenReturn on removeUser(anyString())を呼び出していました。これはwhen(...)メソッドの結果で呼び出されます。 – djointster

1

あなたはできませんdoNothingあなたが何かを返すか、例外をスローいずれかの必要があるため。

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

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