I have some troubles with unit testing a RxJava code part. I want to test the method below. It is a presenter method.
public void onSearchQueryChanged(String searchQuery) {
backendService.getShopResultsCount(searchQuery)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
if (isViewAttached()) {
getView().hideShopSearchInProgress();
getView().displayShopSearchResultCount(result.getSearchResponse().getNumberOfHits());
}
}, error -> {
if (isViewAttached()) {
getView().hideShopSearchInProgress();
}
});
}
In the best case I would like to mock the backendService
and test this usecase for specific search queries and with attached/detached view.
I've done some research and I'm aware of toBlocking()
and test()
methods. They all assume I have the Observable
available. I guess I have to split the method somehow. What would be your approach on that?
My stack: RxJava2, dagger, MVP.
In our project we write code like:
U can workaround schedulers by:
And in test pass
Schedulers.test()
orSchedulers.immediate()
or u cant use
TestRule
withRxJavaPlugins.registerSchedulersHook()