TodoRepositoryImpl implements TodoRepository { @Override public Observable<Todo> createOrUpdate(Todo todo) { return Observable.create(new Observable.OnSubscribe<Todo>() { @Override public void call(Subscriber<? super Todo> subscriber) { try { long id = todo.save(); if (id == -1L) throw new Exception(mContext.getString(R.string.problem_save)); subscriber.onNext(todo); 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 testCreateOrUpdateSuccess() throws Exception { Todo todo = Mockito.mock(Todo.class); when(todo.save()).thenReturn(1L); assertSame(todo, mTodoRepository.createOrUpdate(todo).toBlocking().single()); }
@Test public void testCreateOrUpdateFail() throws Exception { Todo todo = Mockito.mock(Todo.class); when(todo.save()).thenReturn(-1L); mTodoRepository.createOrUpdate(todo).observeOn(Schedulers.immediate()).subscribe(newTodo -> { fail("Should throw exception"); }, throwable -> { assertEquals("Can not save todo", throwable.getMessage()); }); }