无法验证来自RxJava订阅服务器的模拟方法调用

时间:2015-11-27 10:32:08

标签: java android unit-testing retrofit rx-java

我正在尝试在我的Android应用中对演示者进行单元测试。方法我试图测试看起来像这样:

@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是Retrofit接口,mService.search()方法返回RxJava的Observable<SearchResults>。我的单元测试代码如下所示:

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);
    }
}

问题是第三条verify(mMoviesListView, times(1)).showMovies(movies);行 - 它总是失败。我正在尝试调试此测试,我发现控制流程永远不会进入.subscribe(itemPage - {...。我认为这与我订阅Schedulers.io()线程的事实有关,但不知道如何解决这个问题。有任何想法吗? 编辑1: 更改了演示者以将Scheduler作为构造函数参数。将测试更改为如下所示:

@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);
}

仍然收到此测试失败消息:

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)

1 个答案:

答案 0 :(得分:2)

在我的应用程序中,interactors / use-cases / model(在你的情况下是mService)负责为操作指定Scheduler(因为它更了解它的操作类型)。

因此,请将subscribeOn移至mService。之后,你的模拟将正常工作。

更深入,如果现在你想要测试mService我会建议你让它“依赖”Scheduler。换句话说 - 添加Sheduler作为构造函数参数。

public class MyService {

    private final Scheduler taskScheduler;

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

    // ...

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

然后,在测试中,您可以使用Schedulers.immediate()和实际应用Schedulers.io()(或任何您喜欢的,真的)。