Configure WebTestClient for @AutoConfigureMockMvc tests
As of Spring Framework 5.3, `WebTestClient` can now be configured on top of `MockMvc` for testing Spring MVC applications in a mock environment. Prior to this commit, `WebTestClient` would be already configured for WebFlux mock setups with `@AutoConfigureWebTestClient` or live servers (for both MVC and WebFlux apps). This commit enhances the `@AutoConfigureWebMvc` support so that a `WebTestClient` instance is auto-configured if the spring-webflux dependency is present on the classpath. Closes gh-23067
This commit is contained in:
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
@@ -32,8 +33,20 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
class MyMockMvcTests {
|
||||
|
||||
@Test
|
||||
void exampleTest(@Autowired MockMvc mvc) throws Exception {
|
||||
void testWithMockMvc(@Autowired MockMvc mvc) throws Exception {
|
||||
mvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("Hello World"));
|
||||
}
|
||||
|
||||
// If Spring WebFlux is on the classpath, you can drive MVC tests with a WebTestClient
|
||||
@Test
|
||||
void testWithWebTestClient(@Autowired WebTestClient webClient) {
|
||||
// @formatter:off
|
||||
webClient
|
||||
.get().uri("/")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).isEqualTo("Hello World");
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user