Spring-boot: @WebFluxTest Doesn't Import the ErrorWebFluxAutoConfiguration

Created on 19 Mar 2019  路  5Comments  路  Source: spring-projects/spring-boot

Synopsis

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");
    }
}

Suggestion

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.

Related Issues

Apparently supporting WebExceptionHandlers was added in #13627 but the default auto-configuration is missing from the WebFluxTest.

superseded

Most helpful comment

@alimate Yes, please. That would be much appreciated.

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings