Browse Source

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
pull/48295/head
Andy Wilkinson 3 weeks ago
parent
commit
3e82ede609
  1. 6
      documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/spring-boot-applications.adoc
  2. 3
      documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/test-utilities.adoc
  3. 2
      module/spring-boot-resttestclient/build.gradle
  4. 1
      smoke-test/spring-boot-smoke-test-actuator-custom-security/build.gradle
  5. 1
      smoke-test/spring-boot-smoke-test-actuator-extension/build.gradle
  6. 1
      smoke-test/spring-boot-smoke-test-actuator-ui/build.gradle
  7. 1
      smoke-test/spring-boot-smoke-test-actuator/build.gradle
  8. 1
      smoke-test/spring-boot-smoke-test-devtools/build.gradle
  9. 1
      smoke-test/spring-boot-smoke-test-hateoas/build.gradle
  10. 1
      smoke-test/spring-boot-smoke-test-jersey/build.gradle
  11. 1
      smoke-test/spring-boot-smoke-test-jetty-jsp/build.gradle
  12. 1
      smoke-test/spring-boot-smoke-test-jetty-ssl/build.gradle
  13. 1
      smoke-test/spring-boot-smoke-test-jetty/build.gradle
  14. 1
      smoke-test/spring-boot-smoke-test-oauth2-authorization-server/build.gradle
  15. 1
      smoke-test/spring-boot-smoke-test-oauth2-client/build.gradle
  16. 1
      smoke-test/spring-boot-smoke-test-oauth2-resource-server/build.gradle
  17. 1
      smoke-test/spring-boot-smoke-test-prometheus/build.gradle
  18. 1
      smoke-test/spring-boot-smoke-test-quartz/build.gradle
  19. 1
      smoke-test/spring-boot-smoke-test-saml2-service-provider/build.gradle
  20. 1
      smoke-test/spring-boot-smoke-test-secure-jersey/build.gradle
  21. 1
      smoke-test/spring-boot-smoke-test-servlet/build.gradle
  22. 1
      smoke-test/spring-boot-smoke-test-session-jdbc/build.gradle
  23. 1
      smoke-test/spring-boot-smoke-test-tomcat-jsp/build.gradle
  24. 1
      smoke-test/spring-boot-smoke-test-tomcat-multi-connectors/build.gradle
  25. 1
      smoke-test/spring-boot-smoke-test-tomcat-ssl/build.gradle
  26. 2
      smoke-test/spring-boot-smoke-test-tomcat/build.gradle
  27. 1
      smoke-test/spring-boot-smoke-test-traditional/build.gradle
  28. 1
      smoke-test/spring-boot-smoke-test-web-freemarker/build.gradle
  29. 1
      smoke-test/spring-boot-smoke-test-web-groovy-templates/build.gradle
  30. 1
      smoke-test/spring-boot-smoke-test-web-jsp/build.gradle
  31. 1
      smoke-test/spring-boot-smoke-test-web-method-security/build.gradle
  32. 1
      smoke-test/spring-boot-smoke-test-web-mustache/build.gradle
  33. 1
      smoke-test/spring-boot-smoke-test-web-secure-custom/build.gradle
  34. 1
      smoke-test/spring-boot-smoke-test-web-secure-jdbc/build.gradle
  35. 1
      smoke-test/spring-boot-smoke-test-web-secure/build.gradle
  36. 1
      smoke-test/spring-boot-smoke-test-web-static/build.gradle
  37. 3
      smoke-test/spring-boot-smoke-test-web-thymeleaf/build.gradle
  38. 1
      smoke-test/spring-boot-smoke-test-webflux/build.gradle
  39. 1
      system-test/spring-boot-deployment-system-tests/build.gradle

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

@ -197,10 +197,14 @@ include-code::MyRandomPortWebTestClientTests[] @@ -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]]

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

@ -44,6 +44,9 @@ include-code::MyOutputCaptureTests[] @@ -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.

2
module/spring-boot-resttestclient/build.gradle

@ -28,10 +28,10 @@ description = "Spring Boot RestTestClient" @@ -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")

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

@ -28,6 +28,7 @@ dependencies { @@ -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")
}

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

@ -26,6 +26,7 @@ dependencies { @@ -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") {

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

@ -28,6 +28,7 @@ dependencies { @@ -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") {

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

@ -32,6 +32,7 @@ dependencies { @@ -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")
}

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

@ -31,6 +31,7 @@ dependencies { @@ -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") {

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

@ -24,6 +24,7 @@ dependencies { @@ -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") {

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

@ -28,4 +28,5 @@ dependencies { @@ -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"))
}

1
smoke-test/spring-boot-smoke-test-jetty-jsp/build.gradle

@ -39,6 +39,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-jetty-ssl/build.gradle

@ -27,6 +27,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-jetty/build.gradle

@ -27,6 +27,7 @@ dependencies { @@ -27,6 +27,7 @@ dependencies {
}
testImplementation(project(":starter:spring-boot-starter-webmvc-test"))
testImplementation(project(":module:spring-boot-restclient"))
}
tasks.named("compileTestJava") {

1
smoke-test/spring-boot-smoke-test-oauth2-authorization-server/build.gradle

@ -24,6 +24,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-oauth2-client/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-oauth2-resource-server/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-prometheus/build.gradle

@ -30,6 +30,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-quartz/build.gradle

@ -30,6 +30,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-saml2-service-provider/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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")
}

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

@ -26,4 +26,5 @@ dependencies { @@ -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"))
}

1
smoke-test/spring-boot-smoke-test-servlet/build.gradle

@ -25,6 +25,7 @@ dependencies { @@ -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"))

1
smoke-test/spring-boot-smoke-test-session-jdbc/build.gradle

@ -31,6 +31,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-tomcat-jsp/build.gradle

@ -34,6 +34,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-tomcat-multi-connectors/build.gradle

@ -24,6 +24,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-tomcat-ssl/build.gradle

@ -25,6 +25,7 @@ dependencies { @@ -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")
}

2
smoke-test/spring-boot-smoke-test-tomcat/build.gradle

@ -23,8 +23,8 @@ description = "Spring Boot Tomcat smoke test" @@ -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") {

1
smoke-test/spring-boot-smoke-test-traditional/build.gradle

@ -33,6 +33,7 @@ dependencies { @@ -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")

1
smoke-test/spring-boot-smoke-test-web-freemarker/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-web-groovy-templates/build.gradle

@ -28,6 +28,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-web-jsp/build.gradle

@ -34,6 +34,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-web-method-security/build.gradle

@ -27,6 +27,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-web-mustache/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-web-secure-custom/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-web-secure-jdbc/build.gradle

@ -29,6 +29,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-web-secure/build.gradle

@ -26,6 +26,7 @@ dependencies { @@ -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")
}

1
smoke-test/spring-boot-smoke-test-web-static/build.gradle

@ -35,6 +35,7 @@ dependencies { @@ -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") {

3
smoke-test/spring-boot-smoke-test-web-thymeleaf/build.gradle

@ -25,9 +25,8 @@ dependencies { @@ -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") {

1
smoke-test/spring-boot-smoke-test-webflux/build.gradle

@ -25,6 +25,7 @@ dependencies { @@ -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")

1
system-test/spring-boot-deployment-system-tests/build.gradle

@ -41,6 +41,7 @@ dependencies { @@ -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"))

Loading…
Cancel
Save