LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> { public void login() { if(mRootView.getMobileStr().length() != 11){ mRootView.showMessage("手机号码不正确"); return; } if(mRootView.getPassWordStr().length() < 1){ mRootView.showMessage("密码太短"); return; } mModel.login(mRootView.getMobileStr(),mRootView.getPassWordStr()) .compose(RxUtils.applySchedulers(mRootView)) .subscribe(new MyErrorHandleSubscriber<User>(mErrorHandler) { @Override public void onNext(User user) { mRootView.loginSuccess(); } }); } @Inject  LoginPresenter(LoginContract.Model model, LoginContract.View rootView); void login(); @Override void onDestroy();  }
@Test public void loginFailed(){ Mockito.when(view.getMobileStr()).thenReturn("13547250999"); Mockito.when(view.getPassWordStr()).thenReturn("123"); Observable<User> observable = ArmsUtils.obtainAppComponentFromContext( RuntimeEnvironment.application).repositoryManager() .obtainRetrofitService(CommonService.class) .getUser("drchengi"); Mockito.when(model.login(Mockito.anyString(),Mockito.anyString())) .thenReturn(observable); mPresenter.login(); Assert.assertEquals("密码错误", ShadowToast.getTextOfLatestToast()); }
@Test public void loginSuccess(){ Mockito.when(view.getMobileStr()).thenReturn("13547250999"); Mockito.when(view.getPassWordStr()).thenReturn("abc"); Observable<User> observable = ArmsUtils.obtainAppComponentFromContext( RuntimeEnvironment.application).repositoryManager() .obtainRetrofitService(CommonService.class) .getUser("drchengit"); Mockito.when(model.login(Mockito.anyString(),Mockito.anyString())) .thenReturn(observable); mPresenter.login(); Mockito.verify(view).loginSuccess(); }