UserSearchPresenter extends BasePresenter<UserSearchContract.View> implements UserSearchContract.Presenter { @Override public void search(String term) { checkViewAttached(); getView().showLoading(); addSubscription(userRepository.searchUsers(term).subscribeOn(ioScheduler).observeOn(mainScheduler) .subscribe(new Subscriber<List<User>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { getView().hideLoading(); getView().showError( e.getMessage()); } @Override public void onNext(List<User> users) { getView().hideLoading(); getView().showSearchResults(users); } })); } UserSearchPresenter(UserRepository userRepository, Scheduler ioScheduler, Scheduler mainScheduler); @Override void search(String term);  }
@Test public void search_ValidSearchTerm_ReturnsResults() { UsersList userList = getDummyUserList(); when(userRepository.searchUsers(anyString())).thenReturn(Observable.<List<User>>just(userList.getItems())); userSearchPresenter.search("riggaroo"); verify(view).showLoading(); verify(view).hideLoading(); verify(view).showSearchResults(userList.getItems()); verify(view, never()).showError(anyString()); }
@Test public void search_UserRepositoryError_ErrorMsg() { String errorMsg = "No internet"; when(userRepository.searchUsers(anyString())).thenReturn(Observable.error(new IOException(errorMsg))); userSearchPresenter.search("bookdash"); verify(view).showLoading(); verify(view).hideLoading(); verify(view, never()).showSearchResults(anyList()); verify(view).showError(errorMsg); }
@Test(expected = BasePresenter.MvpViewNotAttachedException.class) public void search_NotAttached_ThrowsMvpException() { userSearchPresenter.detachView(); userSearchPresenter.search("test"); verify(view, never()).showLoading(); verify(view, never()).showSearchResults(anyList()); }