RepoListPresenter extends BasePresenter { public void onSearchButtonClick() { String name = view.getUserName(); if (TextUtils.isEmpty(name)) return; showLoadingState(); Subscription subscription = model.getRepoList(name) .map(repoListMapper) .subscribe(new Observer<List<Repository>>() { @Override public void onCompleted() { hideLoadingState(); } @Override public void onError(Throwable e) { hideLoadingState(); showError(e); } @Override public void onNext(List<Repository> list) { if (list != null && !list.isEmpty()) { repoList = list; view.showRepoList(list); } else { view.showEmptyList(); } } }); addSubscription(subscription); } @Inject  RepoListPresenter();  RepoListPresenter(RepoListView view); void onSearchButtonClick(); void onCreateView(Bundle savedInstanceState); void onSaveInstanceState(Bundle outState); void clickRepo(Repository repository);  }
@Test public void testLoadNullData() { doAnswer(invocation -> Observable.just(null)) .when(model) .getRepoList(TestConst.TEST_OWNER); repoListPresenter.onSearchButtonClick(); verify(mockView).showEmptyList(); }
@Test public void testLoadEmptyData() { doAnswer(invocation -> Observable.just(Collections.emptyList())) .when(model) .getRepoList(TestConst.TEST_OWNER); repoListPresenter.onSearchButtonClick(); verify(mockView).showEmptyList(); }
@Test public void testOnError() { doAnswer(invocation -> Observable.error(new Throwable(TestConst.TEST_ERROR))) .when(model) .getRepoList(TestConst.TEST_OWNER); repoListPresenter.onSearchButtonClick(); verify(mockView).showError(TestConst.TEST_ERROR); }
@Test public void testEmptyName() { doAnswer(invocation -> "") .when(mockView) .getUserName(); repoListPresenter.onSearchButtonClick(); verify(mockView, never()).showEmptyList(); verify(mockView, never()).showRepoList(repoList); }
@Test public void testShowLoading() { repoListPresenter.onSearchButtonClick(); verify(mockView).showLoading(); }
@Test public void testHideLoading() { repoListPresenter.onSearchButtonClick(); verify(mockView).hideLoading(); }
@Test public void testShowLoadingOnError() { doAnswer(invocation -> Observable.error(new Throwable(TestConst.TEST_ERROR))) .when(model) .getRepoList(TestConst.TEST_OWNER); repoListPresenter.onSearchButtonClick(); verify(mockView).showLoading(); }
@Test public void testHideLoadingOnError() { doAnswer(invocation -> Observable.error(new Throwable(TestConst.TEST_ERROR))) .when(model) .getRepoList(TestConst.TEST_OWNER); repoListPresenter.onSearchButtonClick(); verify(mockView).hideLoading(); }