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
This commit is contained in:
Andy Wilkinson
2025-11-25 14:38:33 +00:00
parent 0e56cd08ea
commit 3e82ede609
39 changed files with 45 additions and 5 deletions
@@ -197,10 +197,14 @@ include-code::MyRandomPortWebTestClientTests[]
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`.
The `spring-boot-resttestclient` modules also provides a javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] facility:
The `spring-boot-resttestclient` module also provides a javadoc:org.springframework.boot.resttestclient.TestRestTemplate[] facility:
include-code::MyRandomPortTestRestTemplateTests[]
To use `TestRestTemplate` a dependency on `spring-boot-restclient` is also required.
Take care when adding this dependency as it will enable auto-configuration for `RestClient.Builder`.
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.
[[testing.spring-boot-applications.customizing-rest-test-client]]
@@ -44,6 +44,9 @@ include-code::MyOutputCaptureTests[]
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.
It's provided by the `spring-boot-resttestclient` module.
A dependency on `spring-boot-restclient` is also required.
Take care when adding this dependency as it will enable auto-configuration for `RestClient.Builder`.
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.
You can get a vanilla template or one that sends Basic HTTP authentication (with a username and password).
In either case, the template is fault tolerant.
@@ -28,10 +28,10 @@ description = "Spring Boot RestTestClient"
dependencies {
api(project(":core:spring-boot-test"))
api(project(":module:spring-boot-http-converter"))
api(project(":module:spring-boot-restclient"))
api("org.springframework:spring-web")
optional(project(":core:spring-boot-autoconfigure"))
optional(project(":module:spring-boot-restclient"))
optional("org.apache.httpcomponents.client5:httpclient5")
optional("org.jetbrains.kotlin:kotlin-stdlib")
optional("org.jetbrains.kotlin:kotlin-reflect")
@@ -28,6 +28,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testRuntimeOnly("org.apache.httpcomponents.client5:httpclient5")
}
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -28,6 +28,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -32,6 +32,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-actuator-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testRuntimeOnly("org.apache.httpcomponents.client5:httpclient5")
}
@@ -31,6 +31,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-webmvc"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -24,6 +24,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-hateoas"))
testImplementation(project(":starter:spring-boot-starter-hateoas-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -28,4 +28,5 @@ dependencies {
runtimeOnly("jakarta.xml.bind:jakarta.xml.bind-api")
testImplementation(project(":starter:spring-boot-starter-jersey-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
@@ -39,6 +39,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-jetty"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -27,6 +27,7 @@ dependencies {
}
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testRuntimeOnly("org.apache.httpcomponents.client5:httpclient5")
}
@@ -27,6 +27,7 @@ dependencies {
}
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -24,6 +24,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-security-oauth2-authorization-server"))
testImplementation(project(":starter:spring-boot-starter-security-oauth2-authorization-server-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-oauth2-client-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-oauth2-resource-server-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("com.squareup.okhttp3:mockwebserver")
}
@@ -30,6 +30,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-micrometer-metrics-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -30,6 +30,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-quartz-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.awaitility:awaitility")
}
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-saml2-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -26,4 +26,5 @@ dependencies {
implementation(project(":starter:spring-boot-starter-security"))
testImplementation(project(":starter:spring-boot-starter-jersey-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
@@ -25,6 +25,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-security"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation(project(":module:spring-boot-resttestclient"))
testImplementation(project(":starter:spring-boot-starter-test"))
@@ -31,6 +31,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-session-jdbc-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -34,6 +34,7 @@ dependencies {
providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper")
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -24,6 +24,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-webmvc"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -25,6 +25,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-webmvc"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -23,8 +23,8 @@ description = "Spring Boot Tomcat smoke test"
dependencies {
implementation(project(":starter:spring-boot-starter-webmvc"))
testImplementation(project(":module:spring-boot-resttestclient"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -33,6 +33,7 @@ dependencies {
providedRuntime(project(":starter:spring-boot-starter-tomcat-runtime"))
providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper")
testImplementation(project(":module:spring-boot-restclient"))
testImplementation(project(":module:spring-boot-resttestclient"))
testImplementation(project(":starter:spring-boot-starter-test"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-freemarker-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -28,6 +28,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-groovy-templates-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -34,6 +34,7 @@ dependencies {
providedRuntime("org.glassfish.web:jakarta.servlet.jsp.jstl")
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -27,6 +27,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-mustache-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -29,6 +29,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -26,6 +26,7 @@ dependencies {
testImplementation(project(":starter:spring-boot-starter-security-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation("org.apache.httpcomponents.client5:httpclient5")
}
@@ -35,6 +35,7 @@ dependencies {
runtimeOnly("org.webjars:jquery:2.0.3-1")
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -25,9 +25,8 @@ dependencies {
implementation(project(":starter:spring-boot-starter-webmvc"))
implementation(project(":starter:spring-boot-starter-validation"))
testImplementation(project(":module:spring-boot-webmvc-test"))
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":starter:spring-boot-starter-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {
@@ -25,6 +25,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-mustache"))
implementation(project(":starter:spring-boot-starter-webflux"))
testImplementation(project(":module:spring-boot-restclient"))
testImplementation(project(":module:spring-boot-resttestclient"))
testImplementation(project(":starter:spring-boot-starter-webflux-test"))
testImplementation("io.projectreactor:reactor-test")
@@ -41,6 +41,7 @@ dependencies {
implementation(project(":starter:spring-boot-starter-actuator"))
systemTestImplementation(enforcedPlatform(project(path: ":platform:spring-boot-internal-dependencies")))
systemTestImplementation(project(":module:spring-boot-restclient"))
systemTestImplementation(project(":module:spring-boot-resttestclient"))
systemTestImplementation(project(":starter:spring-boot-starter-test"))
systemTestImplementation(project(":test-support:spring-boot-test-support"))