Jepoy
Jepoy

Reputation: 77

Why does the view methods does not get called inside an Observable which is inside the presenter on a unit test?

I am new to Android Unit Testing and we are currently using MVP+RxJava+Dagger 2. I wrote this test which fails in unit test, but works in production code:

@Override
public void retrieveListOfBillers() {
    getMvpView().showLoading();
    getCompositeDisposable().add(
        getDataManager()
            .doServerGetBillersList()
            .observeOn(getSchedulerProvider().ui())
            .subscribeOn(getSchedulerProvider().io())
            .subscribe( response ->{
                for (Datum data : response.getData()) {
                    getMvpView().setUpRecyclerView(enrollmentBillers);
                    getMvpView().showDefaultViews();
                    getMvpView().hideLoading();
                }, throwable -> {
                    ...

And this is how I do it in the test:

@Test
public void testGetListOfBillersCallsSetupRecyclerView(){
    mPresenter.retrieveListOfBillers();
    verify(mView).showLoading();
    verify(mView).setUpRecyclerView(anyList());
}

This is how I instanciated the setup for the test:

@Before
public void setUp() {
    // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
    // inject the mocks in the test the initMocks method needs to be called.
    MockitoAnnotations.initMocks(this);
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    mTestScheduler = new TestScheduler();
    testSchedulerProvider = new TestSchedulerProvider(mTestScheduler);
    mPresenter = new CreateBillerContactPresenter<>(
            dataManager,
            testSchedulerProvider,
            compositeDisposable
    );
    mPresenter.onAttach(mView);


when(dataManager.doServerGetBillersList()).thenReturn(Observable.just(getBillerListResponse));

I believe it has something to do with the TestScheduler but I need someone who actually knows what is the problem here, which is why my test code fails to call setupRecyclerView, and other expected view method calls from the presenter?

Upvotes: 0

Views: 94

Answers (1)

Jepoy
Jepoy

Reputation: 77

I have found the answer: It seems the TestScheduler class have a triggerAction method in which:

"Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or before this Scheduler's present time." -- from comments above the method.

Then the presenter/datamanager calls the view methods as expected.

Upvotes: 1

Related Questions