liveBook · Manning

@AutoConfigureWebTestClient

As explained in Testing Spring Boot Applications

@AutoConfigureWebTestClient is a Spring Boot test annotation that configures a WebTestClient bean for testing a reactive web application.

@AutoConfigureWebTestClient configures a WebTestClient bean for reactive web tests. Combined with @SpringBootTest, it uses the mock web environment, so Netty does not start. section 4.2.2

Use it when testing a WebFlux application through its HTTP-style API:

  • Inject WebTestClient like any other Spring bean. section 4.2.2
  • Issue requests with the fluent get(), uri(), and exchange() API, then assert status and response content. section 4.2.2
  • Use built-in assertions or bridge the response to AssertJ when aligning WebFlux and WebMvc test styles. section 4.2.2
  • Build with WebTestClient.bindToServer() when the test should avoid the mocked network stack. section 4.2.2

Reactive endpoint test

Annotate the test with @SpringBootTest and @AutoConfigureWebTestClient, inject WebTestClient, call client.get().uri("/").exchange(), and verify an OK response containing "Paris (France)". section 4.2.2

Listing 4.10 A WebTestClient-based test
@SpringBootTest
@AutoConfigureWebTestClient #1
class ApiWebClientTests {

  @Autowired
  private WebTestClient client; #2

  @Test
  void indexPageHasSelectedCity() {
    selectCity("Paris");

    client.get()
      .uri("/")
      .exchange() #3
      .expectStatus() #4
      .isOk() #4
      .expectBody(String.class) #4
      .value(body -> #4
        assertThat(body).contains("Paris (France)") #4
      );
  }

}

Shows the complete annotation, injection, request, and response-assertion pattern. section 4.2.2

How Testing Spring Boot Applications covers it

How deep it goes
Explains bean setup, fluent requests, assertions, blocking exchange behavior, and server-bound alternatives.
Examples
Demonstrates a Paris endpoint assertion and a separate request to the Manning terms-of-use page.
What it assumes
Assumes a WebFlux test dependency and familiarity with Spring Boot test annotations and fluent client APIs.
What you can do after
Apply it via Spring Boot annotations to build reactive endpoint tests with fluent response assertions.
How it connects
Connects WebTestClient with WebFlux, SpringBootTest, WebClient, MockMvcTester, and AssertJ.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.