I'm trying to mock a failure of an outbound gateway in a unit test like this:
MessageHandler mockCognitiveAnalyze =
mockMessageHandler().handleNextAndReply(m -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
this.mockIntegrationContext.substituteMessageHandlerFor("cognitiveServicesReadEndpoint", mockCognitiveAnalyze);
What I would expect is that when the cognitiveServicesReadEndpoint is invoked, it won't succeed and the flow should be finished (or in my case, a retry advice is applied so it should be repeated). But what happens is, no exception or error will be thrown and the next handle is invoked. The flow looks like this:
.handle(Http.outboundGateway(cognitiveServicesUri + "/vision/v3.0/read/analyze")
.mappedRequestHeaders("Ocp-Apim-Subscription-Key")
.mappedResponseHeaders("Operation-Location"),
c -> c.advice(this.advices.retryAdvice())
.id("cognitiveServicesReadEndpoint"))
.transform(p -> "")
.handle(Http.outboundGateway(h -> h.getHeaders().get("Operation-Location"))
.mappedRequestHeaders("Ocp-Apim-Subscription-Key")
.httpMethod(HttpMethod.GET)
.expectedResponseType(String.class), this.advices.ocrSpec())
Any idea how can I setup the mock handler to properly throw an exception?
Side note: .transform(p -> "") is needed for proper handling of the headers, see Spring Integration HTTP Outbound Gateway header not forwarder on a consecutive request
UPDATE1
This is how the handler looks at the moment:
.handle(Http.outboundGateway(h -> String.format("%s%s", uri, h.getHeaders()
.get(CustomHeaders.DOCUMENT_ID.name())))
.httpMethod(HttpMethod.GET)
.expectedResponseType(byte[].class), c -> c.advice(this.advices.retryAdvice())
.id("endpoint1"))
.wireTap(sf -> sf.enrichHeaders(h -> h.header("ocp-apim-subscription-key", computerVisionApiKey))
.handle(Http.outboundGateway(cognitiveServicesUri + "/vision/v3.0/read/analyze")
.mappedRequestHeaders("Ocp-Apim-Subscription-Key")
.mappedResponseHeaders("Operation-Location"),
c -> c.advice(this.advices.retryAdvice())
.id("cognitiveServicesReadEndpoint"))
And the testing code:
MessageHandler mockCognitiveAnalyze = mockMessageHandler().handleNext(m -> {
throw new HttpClientErrorException(HttpStatus.UNAUTHORIZED);
});
this.mockIntegrationContext.substituteMessageHandlerFor("cognitiveServicesReadEndpoint",
mockCognitiveAnalyze);