Skip to content

@WebFluxTest Doesn't Import the ErrorWebFluxAutoConfiguration #16258

@alimate

Description

@alimate

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions