TodoRepositoryImpl implements TodoRepository { @Override public Observable<Boolean> delete(Long id) { return Observable.create(new Observable.OnSubscribe<Boolean>() { @Override public void call(Subscriber<? super Boolean> subscriber) { try { Todo todo = Todo.findById(Todo.class, id); if (todo == null) throw new Exception(mContext.getString(R.string.problem_find)); if (!todo.delete()) throw new Exception(mContext.getString(R.string.problem_delete)); subscriber.onNext(true); subscriber.onCompleted(); } catch (Exception ex) { subscriber.onError(ex); } } }); } TodoRepositoryImpl(Context context); @Override Observable<Todo> createOrUpdate(Todo todo); @Override Observable<Boolean> delete(Long id); @Override Observable<List<Todo>> getAll();  }
@Test public void testDeleteNotFound() throws Exception { PowerMockito.when(Todo.findById(Todo.class, 1L)).thenReturn(null); mTodoRepository.delete(1L).observeOn(Schedulers.immediate()).subscribe(newTodo -> { fail("Should throw exception"); }, throwable -> { assertEquals("Can not find todo", throwable.getMessage()); }); }
@Test public void testDeleteFail() throws Exception { Todo todo = Mockito.mock(Todo.class); when(todo.delete()).thenReturn(false); PowerMockito.when(Todo.findById(Todo.class, 1L)).thenReturn(todo); mTodoRepository.delete(1L).observeOn(Schedulers.immediate()).subscribe(newTodo -> { fail("Should throw exception"); }, throwable -> { assertEquals("Can not delete todo", throwable.getMessage()); }); }
@Test public void testDeleteSuccess() throws Exception { Todo todo = Mockito.mock(Todo.class); when(todo.delete()).thenReturn(true); when(Todo.findById(Todo.class, 1L)).thenReturn(todo); assertTrue(mTodoRepository.delete(1L).toBlocking().single()); }