bigywater.tech
Spring · 171

RestTestClient 与 TestRestTemplate:Boot 4 里测 Controller 要加哪个依赖、换哪个包名

升到 Boot 4 后测试里的 TestRestTemplate 和 @AutoConfigureRestTestClient 为什么找不到?要换哪个包、加哪个依赖?RestTestClient 在 MOCK 和 RANDOM_PORT 下有什么不同?这篇在 Spring Boot 4.0.8 上逐个编译、运行。

package … does not exist
旧依赖(starter-test + starter-webmvc)下 @AutoConfigureRestTestClient 编译失败,注解在 spring-boot-resttestclient 模块里
3 个条件
Boot 4 里 TestRestTemplate 要换包名、加 @AutoConfigureTestRestTemplate、再加 starter-restclient 才能用
port=80
MOCK 环境下 RestTestClient 没有启动服务器,响应里的本地端口是 80

不启动服务器

实测 · 本机执行输出
  -- bindToController:不要 Spring 容器
  [旧依赖] bindToController:不启动 Spring 容器,GET /users/1 断言 $.name = user-1 通过  通过
  -- bindToApplicationContext:@SpringBootTest 的容器
  [旧依赖] bindToApplicationContext:响应体 {"id":2,"name":"user-2","port":80}  通过

bindToController 与 bindToApplicationContext 只需要 starter-test。

随机端口

实测 · 本机执行输出
  -- 旧依赖下
  [旧依赖] 编译失败:package org.springframework.boot.resttestclient.autoconfigure does not exist
  -- 新依赖下
  [新依赖] 随机端口 + @AutoConfigureRestTestClient:服务器端口 已分配,响应体 {"id":3,"name":"user-3","port":<端口>},响应里的 port 就是服务器端口  通过

@AutoConfigureRestTestClient 在新模块里,旧依赖编译失败。

MOCK 环境

实测 · 本机执行输出
  -- 新依赖下
  [新依赖] MOCK 环境 + @AutoConfigureRestTestClient:响应体 {"id":4,"name":"user-4","port":80}  通过

同一个注解,MOCK 环境下 port 是 80,没有启动服务器。

断言失败

实测 · 本机执行输出
  -- jsonPath 值不对
  [旧依赖] (没有输出)  [失败,退出码 1:java.lang.AssertionError: JSON path "$.name" expected:<wrong> but was:<user-1>]
  -- 状态码不对(访问不存在的路径)
  [旧依赖] (没有输出)  [失败,退出码 1:java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>]

报错信息带着期望值和实际值。

TestRestTemplate

实测 · 本机执行输出
  -- Boot 3 的写法(org.springframework.boot.test.web.client.TestRestTemplate)
  [旧依赖] 编译失败:package org.springframework.boot.test.web.client does not exist | cannot find symbol
  [新依赖] 编译失败:package org.springframework.boot.test.web.client does not exist | cannot find symbol
  -- 新位置的 TestRestTemplate,没有加 @AutoConfigureTestRestTemplate
  [新依赖] (没有输出)  [失败,退出码 1: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.resttestclient.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Au]
  -- 新位置的 TestRestTemplate,加了 @AutoConfigureTestRestTemplate
  [新依赖] (没有输出)  [失败,退出码 1: java.lang.ClassNotFoundException: org.springframework.boot.restclient.RestTemplateBuilder]
  [新依赖+restclient] {"id":1,"name":"user-1","port":<端口>}  通过

换包名、加注解、加 starter-restclient,三个条件缺一个都不行。

分步演示

RestTestClient
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动(Spring Boot 4.0.8,Spring Framework 7.0.9;JUnit Platform Console Launcher 在独立 JVM 里运行;「旧依赖」是 spring-boot-starter-test 加 spring-boot-starter-webmvc,「新依赖」是 spring-boot-starter-webmvc-test 加 spring-boot-starter-webmvc,再加 spring-boot-starter-restclient 的变体用于 TestRestTemplate;被测接口 GET /users/{id} 返回 id、name 与 request.getLocalPort())
    • 没有验证:webflux 项目里的 WebTestClient 在 Boot 4 的位置;RestTestClient 的 AssertJ 风格断言(RestTestClientResponse);bindToMockMvc、bindToServer 两种绑定;文件上传、Cookie 与表单的测试写法;TestRestTemplate 在 Boot 4 里的认证与重定向行为;为什么 spring-boot-starter-webmvc-test 没带 restclient(只记录了现象)
    京ICP备2026067502号-1京公网安备11010502063487号