Project Reactor onErrorReturn tests not working for errors thrown in nested streams

Viewed 20

I am trying to test a collection of nested reactive services with streams like the following:

Stream A subscribes to Stream B flux, Stream B flux subscribes to Stream C flux, Stream C flux subscribes to Stream D flux.

I have a call like

onErrorReturn( new ResponseObject("test"))

on Stream A to handle any errors emitted from Stream B, C, or D.

I am testing the onErrorReturn() call with a step verifier as follows:

StepVerifier.create( streamACall(testData))
 .expectNextMatches( response -> response.getText() == "test")
 .verify()

I then use Mockito to mock an error being returned from Stream C or D as follows:

Mockito.when(StreamDisCalled()).thenReturn(Flux.error(new someError("Some error message")));

I am expecting this error to then propagate from Stream D, to Stream C, to Stream B, where it is handled by Stream A. However in unit testing I get the following stack trace instead:

expectation "expectNextMatches" failed (expected: onNext(); actual: onError( someError: Some error message))

How can I fix this so that doOnErrorReturn() properly catches and handles the nested error, and my test is able to verify this behavior?

0 Answers
Related