I work on a project with spring-boot (v2 M3) and spring-webflux. While coding a feature, I see that my microservice returns me an Http 200 status code while returning an error in a flux.
So, I make a simple test : I create a simple controller
@RestController
@RequestMapping(value = "/test")
public class TestController {
@GetMapping(value = "/errors")
public Flux<Object> getErrors() {
return Flux.error(new RuntimeException("test"));
}
}
With a simple exception handler :
@ControllerAdvice
public class TestExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(TestExceptionHandler.class);
@ExceptionHandler(value = { RuntimeException.class })
public ResponseEntity<String> handleServerError(final RuntimeException e) {
LOGGER.error(e.getMessage(), e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
And I test the result with the webTestClient in a integration test :
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:test.properties")
public class TestErrorsIT {
@Autowired
private WebTestClient webTestClient;
@Test
public void getErrors() {
this.webTestClient.get().uri("/test/errors").accept(MediaType.APPLICATION_STREAM_JSON).exchange().expectStatus()
.is5xxServerError();
}
}
As a result, my test fails because my controller that returns an error flux returns a 200 status code instead of a 503 status code (the exception log "test" is well traced in my console). Do you know why ?
java.lang.AssertionError: Range for response status value 200 expected: but was:
GET http://localhost:54432/test/errors WebTestClient-Request-Id: [1] Accept: [application/stream+json]
No content
< 200 OK < Content-Type: [application/stream+json] < Transfer-Encoding: [chunked] < Date: [Fri, 03 Nov 2017 09:42:53 GMT]
Content not available yet