As I was experimenting with the @WebFluxTests to test Reactive controllers, I noticed that the provided test _auto-configurations_ do not import the default ErrorWebExceptionHandler, so the following test would fail:
@AutoConfigureErrors
@RunWith(SpringRunner.class)
@WebFluxTest(PersonController.class)
public class ReactiveWebApplicationTests {
@Autowired private WebTestClient webClient;
@Test
public void personName_CanNotBeBlank() {
webClient.post().uri("/persons")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.syncBody("{}")
.exchange()
.expectStatus().isBadRequest()
.expectBody()
.jsonPath("$.errors[0].code").isEqualTo("name.blank");
}
}
This can be easily fixed just by importing the ErrorWebFluxAutoConfiguration:
@AutoConfigureErrors
@RunWith(SpringRunner.class)
@WebFluxTest(PersonController.class)
@ImportAutoConfiguration(ErrorWebFluxAutoConfiguration.class)
public class ReactiveWebApplicationTests {
@Autowired private WebTestClient webClient;
@Test
public void personName_CanNotBeBlank() {
webClient.post().uri("/persons")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.syncBody("{}")
.exchange()
.expectStatus().isBadRequest()
.expectBody()
.jsonPath("$.errors[0].code").isEqualTo("name.blank");
}
}
Although the problem is so easy to fix, that'd be great if we import the default auto-configuration automatically. For example by adding an AutoConfigureErrorWebFlux annotation.
Apparently supporting WebExceptionHandlers was added in #13627 but the default auto-configuration is missing from the WebFluxTest.
Well spotted. This seems like a bug to me, particularly as @WebMvcTest imports the equivalent auto-configuration for the servlet stack. Do you agree, @bclozel?
Yes, we should align with our existing MVC setup here.
@bclozel @wilkinsona May I provide a PR to fix that?
@alimate Yes, please. That would be much appreciated.
Closing in favor of PR #16266.
Most helpful comment
@alimate Yes, please. That would be much appreciated.