vendredi 27 novembre 2015

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?

Aucun commentaire:

Enregistrer un commentaire