Я создал метод, который получает некоторые данные из web-service
, я использую retrofit
, rx
, и я использую juint
и mockito
к испытаниям мой код. Я могу проверить этот метод очень хорошо, но в onCompleted
методы rx
, я звоню другой метод (saveToDatabase)Тест метод, который вызывается из гх
Как я могу отделить эту зависимость? Я не хочу называть этот метод в моем тесте, см мой код ниже:
subscription = githubService.publicRepositories(username)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(application.defaultSubscribeScheduler())
.subscribe(new Subscriber<List<Repository>>()
{
@Override
public void onCompleted()
{
progressVisibility.set(View.INVISIBLE);
if (!repositories.isEmpty())
{
recyclerViewVisibility.set(View.VISIBLE);
}
else
{
infoMessage.set(context.getString(R.string.text_empty_repos));
infoMessageVisibility.set(View.VISIBLE);
}
//THIS METHOD SHOULD NOT BE CALLED IN MY TEST, I WANT TO VERIFY IT
saveToDatabase(MainViewModel.this.repositories);
}
@Override
public void onError(Throwable error)
{
Log.e(TAG, "Error loading GitHub repos ", error);
progressVisibility.set(View.INVISIBLE);
if (isHttp404(error))
{
infoMessage.set(context.getString(R.string.error_username_not_found));
}
else
{
infoMessage.set(context.getString(R.string.error_loading_repos));
}
infoMessageVisibility.set(View.VISIBLE);
}
@Override
public void onNext(List<Repository> repositories)
{
MainViewModel.this.repositories = repositories;
}
});
и это мой тест:
String username = "usernameWithRepos";
TextView textView = new TextView(application);
textView.setText(username);
List<Repository> mockRepos = MockModelFabric.newListOfRepositories(10);
doReturn(rx.Observable.just(mockRepos)).when(githubService).publicRepositories(username);
mainViewModel.onSearchAction(textView, EditorInfo.IME_ACTION_SEARCH, null);
verify(dataListener).onRepositoriesChanged(mockRepos);
assertEquals(mainViewModel.infoMessageVisibility.get(), View.INVISIBLE);
assertEquals(mainViewModel.progressVisibility.get(), View.INVISIBLE);
assertEquals(mainViewModel.recyclerViewVisibility.get(), View.VISIBLE);