Skip to content

Commit 3e82ede

Browse files
committed
Require spring-boot-restclient dependency to use TestRestTemplate
Previously, spring-boot-restclient was a required dependency of spring-boot-resttestclient. This had the unwanted side-effect of increasing the risk of the test classpath enabling auto-configuration for RestClient.Builder when it was main code that needed such a bean. This could lead to integration tests passing but the application itself failing to start when its run through its main method. This commit makes spring-boot-restclient an optional dependency of spring-boot-resttestclient. As a result, a dependency on spring-boot-resttestclient is no longer sufficient to auto-configure a RestClient.Builder bean, although it is still sufficient to auto-configure a RestTestClient bean. Those that wish to use TestRestTemplate rather than migrating to RestTestClient will now have to add a dependency on spring-boot-restclient. This makes it presence more obvious. It now has to be declared directly rather than being somewhat hidden due to being pulled in transitively. The hope is that this will reduce the chances of the dependency being accidentially on the test classpath when main code requires it to be on the runtime classpath. Fixes gh-48253
1 parent 0e56cd0 commit 3e82ede

File tree

39 files changed

+45
-5
lines changed

39 files changed

+45
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,14 @@ include-code::MyRandomPortWebTestClientTests[]
197197

198198
TIP: javadoc:org.springframework.test.web.reactive.server.WebTestClient[] can also used with a xref:testing/spring-boot-applications.adoc#testing.spring-boot-applications.with-mock-environment[mock environment], removing the need for a running server, by annotating your test class with javadoc:org.springframework.boot.webflux.test.autoconfigure.AutoConfigureWebTestClient[format=annotation] from `spring-boot-webflux-test`.
199199

200-
The `spring-boot-resttestclient` modules also provides a javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] facility:
200+
The `spring-boot-resttestclient` module also provides a javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] facility:
201201

202202
include-code::MyRandomPortTestRestTemplateTests[]
203203

204+
To use `TestRestTemplate` a dependency on `spring-boot-restclient` is also required.
205+
Take care when adding this dependency as it will enable auto-configuration for `RestClient.Builder`.
206+
If your main code uses `RestClient.Builder`, declare the `spring-boot-restclient` dependency so that it is on your application's main classpath and not only on its test classpath.
207+
204208

205209

206210
[[testing.spring-boot-applications.customizing-rest-test-client]]

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ include-code::MyOutputCaptureTests[]
4444

4545
javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] is a convenience alternative to Spring's javadoc:org.springframework.web.client.RestTemplate[] that is useful in integration tests.
4646
It's provided by the `spring-boot-resttestclient` module.
47+
A dependency on `spring-boot-restclient` is also required.
48+
Take care when adding this dependency as it will enable auto-configuration for `RestClient.Builder`.
49+
If your main code uses `RestClient.Builder`, declare the `spring-boot-restclient` dependency so that it is on your application's main classpath and not only on its test classpath.
4750

4851
You can get a vanilla template or one that sends Basic HTTP authentication (with a username and password).
4952
In either case, the template is fault tolerant.

module/spring-boot-resttestclient/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ description = "Spring Boot RestTestClient"
2828
dependencies {
2929
api(project(":core:spring-boot-test"))
3030
api(project(":module:spring-boot-http-converter"))
31-
api(project(":module:spring-boot-restclient"))
3231
api("org.springframework:spring-web")
3332

3433
optional(project(":core:spring-boot-autoconfigure"))
34+
optional(project(":module:spring-boot-restclient"))
3535
optional("org.apache.httpcomponents.client5:httpclient5")
3636
optional("org.jetbrains.kotlin:kotlin-stdlib")
3737
optional("org.jetbrains.kotlin:kotlin-reflect")

smoke-test/spring-boot-smoke-test-actuator-custom-security/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828

2929
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
3030
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
31+
testImplementation(project(":module:spring-boot-restclient"))
3132

3233
testRuntimeOnly("org.apache.httpcomponents.client5:httpclient5")
3334
}

smoke-test/spring-boot-smoke-test-actuator-extension/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626

2727
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
2828
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
29+
testImplementation(project(":module:spring-boot-restclient"))
2930
}
3031

3132
tasks.named("compileTestJava") {

smoke-test/spring-boot-smoke-test-actuator-ui/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828

2929
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
3030
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
31+
testImplementation(project(":module:spring-boot-restclient"))
3132
}
3233

3334
tasks.named("compileTestJava") {

smoke-test/spring-boot-smoke-test-actuator/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies {
3232

3333
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
3434
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
35+
testImplementation(project(":module:spring-boot-restclient"))
3536

3637
testRuntimeOnly("org.apache.httpcomponents.client5:httpclient5")
3738
}

smoke-test/spring-boot-smoke-test-devtools/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
implementation(project(":starter:spring-boot-starter-webmvc"))
3232

3333
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
34+
testImplementation(project(":module:spring-boot-restclient"))
3435
}
3536

3637
tasks.named("compileTestJava") {

smoke-test/spring-boot-smoke-test-hateoas/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
implementation(project(":starter:spring-boot-starter-hateoas"))
2525

2626
testImplementation(project(":starter:spring-boot-starter-hateoas-test"))
27+
testImplementation(project(":module:spring-boot-restclient"))
2728
}
2829

2930
tasks.named("compileTestJava") {

smoke-test/spring-boot-smoke-test-jersey/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ dependencies {
2828
runtimeOnly("jakarta.xml.bind:jakarta.xml.bind-api")
2929

3030
testImplementation(project(":starter:spring-boot-starter-jersey-test"))
31+
testImplementation(project(":module:spring-boot-restclient"))
3132
}

0 commit comments

Comments
 (0)