NewsViewModel extends BaseViewModel { public void createNewsPagedList() { if (newsPagedListLiveData.getValue() == null){ execute( disposable -> pagingDataStatus.postValue(DataStatus.LOADING), newsUIPagedList -> { newsPagedListLiveData.postValue(newsUIPagedList); pagingDataStatus.setValue(DataStatus.SUCCESS); }, throwable -> pagingDataStatus.postValue(DataStatus.ERROR), newsUseCase.getNewsPagedList() ); } } @Inject  NewsViewModel(@Named(value = IO_SCHEDULER) Scheduler subscribeOn, @Named(value = MAIN_THREAD_SCHEDULER) Scheduler observeOn, GetNewsUseCase<NewsUI> newsUsecase, PagingState pagingState); void createNewsPagedList(); void loadNews(int pageNumber); LiveData<PagedList<NewsUI>> getNewsPagedListLiveData(); SingleLiveEvent<DataStatus> getLoadNewsStats(); LiveData<DataStatus> getPagingDataStatus(); PagingState getPagingState();  }
@Test public void verify_create_paged_list_called_only_once_in_success() throws InterruptedException { when(usecase.getNewsPagedList()).thenReturn(Observable.just(pagedList)); verify(usecase, times(0)).getNewsPagedList(); viewModel.createNewsPagedList(); verify(usecase, times(1)).getNewsPagedList(); viewModel.createNewsPagedList(); verify(usecase, times(1)).getNewsPagedList(); }
@Test public void verify_create_paged_list_called_every_time_in_error() throws InterruptedException { when(usecase.getNewsPagedList()).thenReturn(Observable.create(emitter -> { emitter.onError(new NullPointerException()); })); verify(usecase, times(0)).getNewsPagedList(); viewModel.createNewsPagedList(); verify(usecase, times(1)).getNewsPagedList(); viewModel.createNewsPagedList(); verify(usecase, times(2)).getNewsPagedList(); viewModel.createNewsPagedList(); verify(usecase, times(3)).getNewsPagedList(); }