@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
