Siv*_*mar 1 java junit mockito
public interface ABC {
public boolean removeUser(String userId) throws OTPServiceException, RemoteException;
}
ABC abc= mock(ABC.class);
doNothing().when(abc).removeUser(anyString());
Run Code Online (Sandbox Code Playgroud)
我试过这样的.我得到了以下异常.
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
Run Code Online (Sandbox Code Playgroud)
小智 7
您的方法返回一个布尔值,因此您应该模拟一个布尔响应.
你应该有这样的东西:
when(abc.removeUser(anyString())).thenReturn(true);
Run Code Online (Sandbox Code Playgroud)
您可以在mockito中检查doThrow()doAnswer()doNothing()和doReturn()的用法,以获得更详细和简单的解释.
您不能doNothing
使用非void
方法,因为您需要返回某些内容或抛出异常。
when(abc.removeUser(anyString())).thenReturn(true);
when(abc.removeUser(anyString())).thenThrow(RuntimeException.class);
Run Code Online (Sandbox Code Playgroud)