SearchViewModel extends BaseViewModel { public void searchFor(String query) { if (searchQueryState == null){ createPagedList(query); }else { searchNewsUseCase.updateSearchQuery(query); } searchQueryState = query; } @Inject  SearchViewModel(@Named(value = IO_SCHEDULER) Scheduler subscribeOn, @Named(value = MAIN_THREAD_SCHEDULER) Scheduler observeOn, SearchNewsUseCase<NewsUI> searchNewsUseCase); void searchFor(String query); LiveData<PagedList<NewsUI>> getNewsLiveData(); MutableLiveData<DataStatus> getPagingDataStatus(); String getSearchQueryState();  }
@Test public void verify_getSearchPagedList_called_only_once_time() { when(usecase.getSearchPagedList(query)).thenReturn(Observable.just(pagedList)); verify(usecase, times(0)).getSearchPagedList(query); viewModel.searchFor(query); verify(usecase, times(1)).getSearchPagedList(query); viewModel.searchFor(query); verify(usecase, times(1)).getSearchPagedList(query); }
@Test public void verify_updateSearchQuery_not_called_in_first_time(){ when(usecase.getSearchPagedList(query)).thenReturn(Observable.just(pagedList)); viewModel.searchFor(query); verify(usecase, times(0)).updateSearchQuery(query); }
@Test public void verify_updateSearchQuery_called_after_first_time() { when(usecase.getSearchPagedList(query)).thenReturn(Observable.just(pagedList)); viewModel.searchFor(query); viewModel.searchFor(query); verify(usecase, times(1)).updateSearchQuery(query); viewModel.searchFor(query); verify(usecase, times(2)).updateSearchQuery(query); viewModel.searchFor(query); verify(usecase, times(3)).updateSearchQuery(query); }