Skip to content

Commit 7ff70ca

Browse files
committed
Document that web clients are opt-in with @SpringBootTest
Closes gh-47891
1 parent 6b3edf8 commit 7ff70ca

File tree

8 files changed

+22
-4
lines changed

8 files changed

+22
-4
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/spring-boot-applications.adoc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,20 @@ If you use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)`, an avai
178178

179179
The javadoc:org.springframework.boot.test.web.server.LocalServerPort[format=annotation] annotation can be used to xref:how-to:webserver.adoc#howto.webserver.discover-port[inject the actual port used] into your test.
180180

181-
For convenience, tests that need to make REST calls to the started server can additionally autowire a
182-
{url-spring-framework-docs}/testing/resttestclient.html[`RestTestClient`] which resolves relative links to the running server and comes with a dedicated API for verifying responses, as shown in the following example:
181+
Tests that need to make REST calls to the started server can autowire a
182+
{url-spring-framework-docs}/testing/resttestclient.html[`RestTestClient`] by annotating the test class with javadoc:org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient[format=annotation].
183+
184+
The configured client resolves relative links to the running server and comes with a dedicated API for verifying responses, as shown in the following example:
183185

184186
include-code::MyRandomPortRestTestClientTests[]
185187

186188
If you prefer to use AssertJ, dedicated assertions are available from javadoc:org.springframework.test.web.servlet.client.assertj.RestTestClientResponse[], as shown in the following example:
187189

188190
include-code::MyRandomPortRestTestClientAssertJTests[]
189191

190-
If you have `spring-webflux` on the classpath, you can also autowire a {url-spring-framework-docs}/testing/webtestclient.html[`WebTestClient`] that provides a similar API:
192+
If you have `spring-webflux` on the classpath, you can also autowire a {url-spring-framework-docs}/testing/webtestclient.html[`WebTestClient`] by annotating the test class with javadoc:org.springframework.boot.webtestclient.AutoConfigureWebTestClient[format=annotation].
193+
194+
`WebTestClient` provides a similar API, as shown in the following example:
191195

192196
include-code::MyRandomPortWebTestClientTests[]
193197

@@ -387,6 +391,7 @@ Often, javadoc:org.springframework.boot.webflux.test.autoconfigure.WebFluxTest[f
387391
javadoc:org.springframework.boot.webflux.test.autoconfigure.WebFluxTest[format=annotation] also auto-configures {url-spring-framework-docs}/testing/webtestclient.html[`WebTestClient`], which offers a powerful way to quickly test WebFlux controllers without needing to start a full HTTP server.
388392

389393
TIP: You can also auto-configure javadoc:org.springframework.test.web.reactive.server.WebTestClient[] in a non-`@WebFluxTest` (such as javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation]) by annotating it with javadoc:org.springframework.boot.webflux.test.autoconfigure.AutoConfigureWebTestClient[format=annotation].
394+
390395
The following example shows a class that uses both javadoc:org.springframework.boot.webflux.test.autoconfigure.WebFluxTest[format=annotation] and a javadoc:org.springframework.test.web.reactive.server.WebTestClient[]:
391396

392397
include-code::MyControllerTests[]

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/test-utilities.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] can be instan
6262

6363
include-code::MyTests[]
6464

65-
Alternatively, if you use the javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation] annotation with `WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can inject a fully configured javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] by by annotating your test class with javadoc:org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate[format=annotation] and start using it.
65+
Alternatively, if you use the javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation] annotation with `WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can inject a fully configured javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] by annotating the test class with javadoc:org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate[format=annotation].
6666
If necessary, additional customizations can be applied through the javadoc:org.springframework.boot.web.client.RestTemplateBuilder[] bean.
67+
6768
Any URLs that do not specify a host and port automatically connect to the embedded server, as shown in the following example:
6869

6970
include-code::MySpringBootTests[]

documentation/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/withmockenvironment/MyMockMvcTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
2323
import org.springframework.boot.test.context.SpringBootTest;
2424
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
25+
import org.springframework.boot.webtestclient.AutoConfigureWebTestClient;
2526
import org.springframework.test.web.reactive.server.WebTestClient;
2627
import org.springframework.test.web.servlet.MockMvc;
2728
import org.springframework.test.web.servlet.assertj.MockMvcTester;
@@ -37,6 +38,7 @@
3738
@SpringBootTest
3839
@AutoConfigureMockMvc
3940
@AutoConfigureRestTestClient
41+
@AutoConfigureWebTestClient
4042
class MyMockMvcTests {
4143

4244
@Test

documentation/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/withrunningserver/MyRandomPortTestRestTemplateTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.resttestclient.TestRestTemplate;
23+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
2324
import org.springframework.boot.test.context.SpringBootTest;
2425
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
2728

2829
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
30+
@AutoConfigureTestRestTemplate
2931
class MyRandomPortTestRestTemplateTests {
3032

3133
@Test

documentation/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/withrunningserver/MyRandomPortWebTestClientTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.test.context.SpringBootTest;
2323
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
24+
import org.springframework.boot.webtestclient.AutoConfigureWebTestClient;
2425
import org.springframework.test.web.reactive.server.WebTestClient;
2526

2627
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
28+
@AutoConfigureWebTestClient
2729
class MyRandomPortWebTestClientTests {
2830

2931
@Test

documentation/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/springbootapplications/withmockenvironment/MyMockMvcTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired
2222
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient
2323
import org.springframework.boot.test.context.SpringBootTest
2424
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc
25+
import org.springframework.boot.webtestclient.AutoConfigureWebTestClient
2526
import org.springframework.test.web.reactive.server.WebTestClient
2627
import org.springframework.test.web.reactive.server.expectBody
2728
import org.springframework.test.web.servlet.MockMvc
@@ -36,6 +37,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
3637
@SpringBootTest
3738
@AutoConfigureMockMvc
3839
@AutoConfigureRestTestClient
40+
@AutoConfigureWebTestClient
3941
class MyMockMvcTests {
4042

4143
@Test

documentation/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/springbootapplications/withrunningserver/MyRandomPortTestRestTemplateTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import org.springframework.beans.factory.annotation.Autowired
2222
import org.springframework.boot.test.context.SpringBootTest
2323
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
2424
import org.springframework.boot.resttestclient.TestRestTemplate
25+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate
2526

2627
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
28+
@AutoConfigureTestRestTemplate
2729
class MyRandomPortTestRestTemplateTests {
2830

2931
@Test

documentation/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/springbootapplications/withrunningserver/MyRandomPortWebTestClientTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import org.junit.jupiter.api.Test
2020
import org.springframework.beans.factory.annotation.Autowired
2121
import org.springframework.boot.test.context.SpringBootTest
2222
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
23+
import org.springframework.boot.webtestclient.AutoConfigureWebTestClient
2324
import org.springframework.test.web.reactive.server.WebTestClient
2425
import org.springframework.test.web.reactive.server.expectBody
2526

2627
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
28+
@AutoConfigureWebTestClient
2729
class MyRandomPortWebTestClientTests {
2830

2931
@Test

0 commit comments

Comments
 (0)