2

I wanna test my Presenter

public class MainPresenter extends MvpBasePresenter<MainView> {

    private Repository repository;

    private final CompositeDisposable disposables = new CompositeDisposable();

    public void setRepository(Repository repository) {
        this.repository = repository;
    }

    public void loadFromRepository() {
        getView().showLoading(false);

        disposables.add(repository.getCountries()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribeWith(new DisposableObserver<List<Country>>() {
                    @Override
                    public void onNext(List<Country> countries) {
                        if (isViewAttached()) {
                            getView().setData(countries);
                            getView().showContent();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isViewAttached()) {
                            getView().showError(e, false);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                }));
    }

    public void loadFromRemoteDatastore() {
        disposables.add(new RemoteDataStore().getCountries()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribeWith(new DisposableObserver<List<Country>>() {
                    @Override
                    public void onNext(List<Country> countries) {
                        if (isViewAttached()) {
                            getView().setData(countries);
                            getView().showContent();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isViewAttached()) {
                            getView().showError(e, false);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                }));
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
        if (!retainInstance) {
            disposables.clear();
        }
    }
}

However, I have many doubts, what's the best way to test it

1) Is this alright if I will write these 4 test scenarios

shouldShowContentWhenLoadFromRepository()
shouldShowErrorWhenLoadFromRepository()
shouldShowContentWhenLoadFromRemoteDatastore()
shouldShowErrorWhenLoadFromRemoteDatastore()

2) Should I write a test for detachView(boolean retainInstance) and clear disposables

3) What kind of mechanisms are the best in my case to test RxJava?

jakub
  • 3,576
  • 3
  • 29
  • 55
  • Does your MainPresenter have any non void methods to the outside? If you want to test the behaviour of your Presenter you would need a stream of Events, like LoadingDataFailedEvent, which you could subscribe to from outside and test it, if mocked repository return Obs.error(). – Sergej Isbrecht Apr 22 '17 at 13:07
  • Thank you for the answer, Hans. I posted my whole Presenter, I have only void methods. Don't get it, to be honest. By any chance, could you write sample code, post a link to any open source code or article that explains what do you mean? – jakub Apr 22 '17 at 13:34

1 Answers1

0
  1. Those test scenarios seem reasonable.

  2. It's often good practice to have tests covering an object's public surface, but testing that detachView() clears disposables may be tricky given the current implementation of MainPresenter.

  3. You could create a stub for Repository the returns an Observable returned when GetCountries() is called. You can create both successful and unsuccessful Observables by using Observable.return() & Observable.error() respectively.

If you need more control over the specific timing of the asynchrony the TestScheduler makes it very simple (seen here).

yohanmishkin
  • 158
  • 12