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:
@@ -172,8 +172,9 @@ include::{docs-java}/features/testing/springbootapplications/usingapplicationarg
|
||||
|
||||
[[features.testing.spring-boot-applications.with-mock-environment]]
|
||||
==== Testing with a mock environment
|
||||
By default, `@SpringBootTest` does not start the server.
|
||||
If you have web endpoints that you want to test against this mock environment, you can additionally configure {spring-framework-docs}/testing.html#spring-mvc-test-framework[`MockMvc`] as shown in the following example:
|
||||
By default, `@SpringBootTest` does not start the server but instead sets up a mock environment for testing web endpoints.
|
||||
|
||||
With Spring MVC, we can query our web endpoints using {spring-framework-docs}/testing.html#spring-mvc-test-framework[`MockMvc`] or `WebTestClient`, as shown in the following example:
|
||||
|
||||
[source,java,indent=0,subs="verbatim"]
|
||||
----
|
||||
@@ -182,7 +183,7 @@ include::{docs-java}/features/testing/springbootapplications/withmockenvironment
|
||||
|
||||
TIP: If you want to focus only on the web layer and not start a complete `ApplicationContext`, consider <<features#features.testing.spring-boot-applications.spring-mvc-tests,using `@WebMvcTest` instead>>.
|
||||
|
||||
Alternatively, you can configure a {spring-framework-docs}/testing.html#webtestclient-tests[`WebTestClient`] as shown in the following example:
|
||||
With Spring WebFlux endpoints, you can use {spring-framework-docs}/testing.html#webtestclient-tests[`WebTestClient`] as shown in the following example:
|
||||
|
||||
[source,java,indent=0,subs="verbatim"]
|
||||
----
|
||||
@@ -214,6 +215,8 @@ For convenience, tests that need to make REST calls to the started server can ad
|
||||
include::{docs-java}/features/testing/springbootapplications/withrunningserver/MyRandomPortWebTestClientTests.java[]
|
||||
----
|
||||
|
||||
TIP: `WebTestClient` can be used against both live servers and <<features#features.testing.spring-boot-applications.with-mock-environment, mock environments>>.
|
||||
|
||||
This setup requires `spring-webflux` on the classpath.
|
||||
If you can't or won't add webflux, Spring Boot also provides a `TestRestTemplate` facility:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user