WelcomePresenter extends BasePresenter<WelcomeMvpView> { public void getAccessToken(String code) { checkViewAttached(); getMvpView().showLoadingState(true); mSubscription = mDataManager.getAccessToken(code) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new SingleSubscriber<AccessToken>() { @Override public void onSuccess(AccessToken accessToken) { getMvpView().showLoadingState(false); getMvpView().launchMainActivity(); } @Override public void onError(Throwable error) { getMvpView().showLoadingState(false); Timber.e(error, "There was a problem retrieving the access token"); getMvpView().showAccessTokenError(); } }); } @Inject  WelcomePresenter(DataManager dataManager); @Override void detachView(); void getAccessToken(String code);  }
@Test public void getAccessTokenSucceedsAndLaunchesMainActivity() { AccessToken accessToken = TestDataFactory.makeAccessToken(); stubDataManagerGetAccessToken(Single.just(accessToken)); mWelcomePresenter.getAccessToken("code"); verify(mMockWelcomeMvpView).showLoadingState(true); verify(mMockWelcomeMvpView).launchMainActivity(); verify(mMockWelcomeMvpView).showLoadingState(false); }
@Test public void getAccountDataFailsAndShowsErrorMessage() { stubDataManagerGetAccessToken(Single.<AccessToken>error(new RuntimeException())); mWelcomePresenter.getAccessToken("code"); verify(mMockWelcomeMvpView).showLoadingState(true); verify(mMockWelcomeMvpView).showAccessTokenError(); verify(mMockWelcomeMvpView, never()).launchMainActivity(); verify(mMockWelcomeMvpView).showLoadingState(false); }