Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't verify mock method call from RxJava Subscriber

I'm trying to unit test presenter in my Android app. Method I'm trying to test looks like this:

@Override
public boolean loadNextPage() {
    if (!mIsLoading) {
        mIsLoading = true;
        if (mViewReference.get() != null) {
            mViewReference.get().showProgress();
        }
        mService.search(mSearchQuery, ++mCurrentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(itemsPage -> {
                            mIsLoading = false;
                            mTotalPages = itemsPage.getPagination().getTotalPages();
                            if (mViewReference.get() != null) {
                                mViewReference.get().showMovies(itemsPage.getItems());
                            }
                        },
                        error -> {
                            mIsLoading = false;
                            Log.d(LOG_TAG, error.toString());
                        });
    }
    return mTotalPages == 0 || mCurrentPage < mTotalPages;
}

mService is Retrofit interface and mService.search() method returns RxJava's Observable<SearchResults>. My unit test code looks like this:

package mobi.zona.presenters;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;

import com.example.api.Service;
import com.example.model.Movie;
import com.example.model.SearchResults;
import com.example.views.MoviesListView;
import rx.Observable;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class SearchPresenterTest {

    @Mock
    Service mService;
    @Mock
    MoviesListView mMoviesListView;

    @Test
    public void testLoadNextPage() throws Exception {
        String searchQuery = "the hunger games";
        SearchResults searchResults = new SearchResults();
        List<Movie> movies = new ArrayList<>();
        searchResults.setItems(movies);

        when(mService.search(searchQuery, 1)).thenReturn(Observable.just(new SearchResults()));

        MoviesListPresenter presenter = new SearchPresenter(mZonaService, mMoviesListView, searchQuery);
        presenter.loadNextPage();

        verify(mService, times(1)).search(searchQuery, 1);
        verify(mMoviesListView, times(1)).showProgress();
        verify(mMoviesListView, times(1)).showMovies(movies);
    }
}

The problem is the third verify(mMoviesListView, times(1)).showMovies(movies); line - it allways fails. Whem I'm trying to debug this test I see that control flow never goes into .subscribe(itemPage - {.... I think that it's something related to the fact that I'm subscribing on Schedulers.io() thread, but have no idea on how to fix this. Any ideas? EDIT 1: Changed the presenter to take Scheduler's as constructor parameters. Changed test to look like this:

@Test
public void testLoadNextPage() throws Exception {
    String searchQuery = "the hunger games";
    SearchResults searchResults = new SearchResults();
    List<Movie> movies = new ArrayList<>();
    searchResults.setItems(movies);

    when(mZonaService.search(searchQuery, 1)).thenReturn(Observable.just(new SearchResults()));

    MoviesListPresenter presenter = new SearchPresenter(mZonaService, mMoviesListView, searchQuery,
            Schedulers.test(), Schedulers.test());
    presenter.loadNextPage();

    verify(mZonaService, times(1)).search(searchQuery, 1);
    verify(mMoviesListView, times(1)).showProgress();
    verify(mMoviesListView, times(1)).showMovies(movies);
}

Still getting this test failure message:

Wanted but not invoked:
mMoviesListView.showMovies([]);
-> at com.example.presenters.SearchPresenterTest.testLoadNextPage(SearchPresenterTest.java:46)

However, there were other interactions with this mock:
mMoviesListView.showProgress();
-> at com.example.presenters.SearchPresenter.loadNextPage(SearchPresenter.java:41)
like image 833
Fyodor Sherstobitov Avatar asked Nov 27 '15 10:11

Fyodor Sherstobitov


1 Answers

In my apps interactors/use-cases/model (mService in your case) is responsible for specifying Scheduler for the operation (since it knows better what kind of operation it does).

So, move your subscribeOn to mService. After that your mock will work fine.

Going deeper, if now you'll want to test mService I would recommend you to make it "dependent" on Scheduler. In other words - add Sheduler as a constructor parameter.

public class MyService {

    private final Scheduler taskScheduler;

    public MyService(Scheduler taskScheduler) {
        this.taskScheduler = taskScheduler;
    }

    // ...

    public Observable<Something> query() {
        return someObservable.subscribeOn(taskScheduler);
    }
}

Then, in tests you can use Schedulers.immediate() and for actual app Schedulers.io() (or whatever you like, really).

like image 96
Dmitry Zaytsev Avatar answered Oct 10 '22 07:10

Dmitry Zaytsev