我想测试用户登录在mockito中失败的时间
我的界面
public interface OnLoginFinishListener {
void onFail();
void onSuccess();
}
我的LoginInteractor类
public class LoginInteractor {
public void validateCredentialsAsync(final String email, final String password, final OnLoginFinishListener listener) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(email.equals("sample@email.com") && password.equals("123456")){
listener.onSuccess();
}else {
listener.onFail();
}
}
}, 5000);
}
}
如何为登录失败案例编写测试,谢谢
答案 0 :(得分:2)
可能是你想要模拟你的OnLoginFinishListener并验证它是否调用
LoginInteractor in = new LoginInteractor();
OnLoginFinishListener mockListner = mock(OnLoginFinishListener.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
// TODO Auto-generated method stub
return null;
}
}).when(mockListner).onFail();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
// TODO Auto-generated method stub
return null;
}
}).when(mockListner).onSuccess();
in.validateCredentialsAsync("sample@email.com","wrong",mockListner);
verify(mockListner,times(1)).onFail();
verify(mockListner,never()).onSuccess();