@Test public void testCheckChangeItemFail() throws Exception { when(mRepository.createOrUpdate(any(Todo.class))) .thenReturn( Observable.create( (Observable.OnSubscribe<Todo>) subscriber -> { try { throw new Exception("error"); } catch (Exception ex) { subscriber.onError(ex); } })); fetchData(); mViewModel .checkChangeItem(mItemViewModelList.get(0)) .subscribeOn(Schedulers.immediate()) .subscribe( todo -> { fail("Should not success"); }, throwable -> { assertEquals("error", throwable.getMessage()); }); assertFalse(mItemViewModelList.get(0).isCompleted()); }
@Test public void testDeleteItemFail() throws Exception { when(mRepository.delete(anyLong())).thenReturn(Observable.just(false)); fetchData(); mViewModel.deleteItem(0).toBlocking().single(); assertThat(mViewModel.getTodoItems()).hasSize(3); assertEquals(View.GONE, mViewModel.getAlertVisibility().get()); }
@Test public void testDeleteItemSuccess() throws Exception { when(mRepository.delete(anyLong())).thenReturn(Observable.just(true)); fetchData(); for (int i = 0; i < 3; i++) mViewModel.deleteItem(0).toBlocking().single(); assertThat(mViewModel.getTodoItems()).hasSize(0); assertEquals(View.VISIBLE, mViewModel.getAlertVisibility().get()); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mRepository.getAll()).thenReturn(Observable.just(DataUtils.provideMockTodoList())); mViewModel = new TodoListViewModelImpl( RuntimeEnvironment.application, mRepository, new RxUtils.SchedulerHolder(Schedulers.immediate(), Schedulers.immediate())); }
@Test public void testCheckChangeItemSuccess() throws Exception { when(mRepository.createOrUpdate(any(Todo.class))) .thenAnswer( invocation -> { Object[] args = invocation.getArguments(); return Observable.just((Todo) args[0]); }); fetchData(); mViewModel.checkChangeItem(mItemViewModelList.get(0)).toBlocking().single(); assertTrue(mItemViewModelList.get(0).isCompleted()); }
@Test public void testGetAlertVisibilityVisibleNoItem() throws Exception { when(mRepository.getAll()).thenReturn(Observable.just(DataUtils.provideEmptyList())); mViewModel.fetchAllTodo().toBlocking().single(); assertEquals(View.VISIBLE, mViewModel.getAlertVisibility().get()); }