### Question:
SingleOnSubscribeExecuteAsBlockingOptional implements SingleOnSubscribe<Optional<Result>> { @Override public void subscribe(@NonNull SingleEmitter<Optional<Result>> emitter) throws Exception { try { final Result value = preparedOperation.executeAsBlocking(); emitter.onSuccess(Optional.of(value)); } catch (Exception e) { emitter.onError(e); } } SingleOnSubscribeExecuteAsBlockingOptional(@NonNull PreparedOperation<Result, Optional<Result>, Data> preparedOperation); @Override void subscribe(@NonNull SingleEmitter<Optional<Result>> emitter);  }### Answer:
@SuppressWarnings("CheckResult") @Test public void shouldExecuteAsBlockingAfterSubscription() { final PreparedOperation<String, Optional<String>, String> preparedOperation = mock(PreparedOperation.class); String expectedResult = "test"; when(preparedOperation.executeAsBlocking()).thenReturn(expectedResult); TestObserver<Optional<String>> testObserver = new TestObserver<Optional<String>>(); verifyZeroInteractions(preparedOperation); Single<Optional<String>> single = Single.create(new SingleOnSubscribeExecuteAsBlockingOptional<String, String>(preparedOperation)); verifyZeroInteractions(preparedOperation); single.subscribe(testObserver); testObserver.assertValue(Optional.of(expectedResult)); testObserver.assertNoErrors(); testObserver.assertComplete(); verify(preparedOperation).executeAsBlocking(); }
@SuppressWarnings("CheckResult") @Test public void shouldCallOnErrorIfExceptionOccurred() { final PreparedOperation<Object, Optional<Object>, Object> preparedOperation = mock(PreparedOperation.class); StorIOException expectedException = new StorIOException("test exception"); when(preparedOperation.executeAsBlocking()).thenThrow(expectedException); TestObserver<Optional<Object>> testObserver = new TestObserver<Optional<Object>>(); Single<Optional<Object>> single = Single.create(new SingleOnSubscribeExecuteAsBlockingOptional<Object, Object>(preparedOperation)); verifyZeroInteractions(preparedOperation); single.subscribe(testObserver); testObserver.assertError(expectedException); testObserver.assertNotComplete(); verify(preparedOperation).executeAsBlocking(); }