bigywater.tech
Spring · 173

Boot 4 的 @WebMvcTest、@DataJpaTest 为什么编译不过:新包名与每个注解对应的 test starter

Boot 4 升级后 @WebMvcTest、@DataJpaTest 为什么 package does not exist?新包名是什么,要加哪个 test starter?spring-boot-starter-test-classic 能不能当过渡?这篇在 Spring Boot 4.0.8 上逐个编译、运行。

10 / 14
统计的 14 个常用测试注解里,有 10 个在 4.0.8 里换了包,4 个没变
8 / 9
统计的 9 个切片类注解里,8 个要额外的 test starter,只有 @JsonTest 随 spring-boot-starter-test 自带
classic 不保留旧包名
spring-boot-starter-test-classic 带上了所有测试模块,但 Boot 3 的 import 仍然编译失败

包名对照

实测 · 本机执行输出
  @WebMvcTest:3.5.16 = org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;4.0.8 = org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest
  @DataJpaTest:3.5.16 = org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;4.0.8 = org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest
  @JdbcTest:3.5.16 = org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;4.0.8 = org.springframework.boot.jdbc.test.autoconfigure.JdbcTest
  @JsonTest:3.5.16 = org.springframework.boot.test.autoconfigure.json.JsonTest;4.0.8 = org.springframework.boot.test.autoconfigure.json.JsonTest(没变)
  @AutoConfigureMockMvc:3.5.16 = org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;4.0.8 = org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc
  @WebFluxTest:3.5.16 = org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;4.0.8 = org.springframework.boot.webflux.test.autoconfigure.WebFluxTest
  @AutoConfigureWebTestClient:3.5.16 = org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;4.0.8 = org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient
  @DataRedisTest:3.5.16 = org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;4.0.8 = org.springframework.boot.data.redis.test.autoconfigure.DataRedisTest
  @RestClientTest:3.5.16 = org.springframework.boot.test.autoconfigure.web.client.RestClientTest;4.0.8 = org.springframework.boot.restclient.test.autoconfigure.RestClientTest
  @AutoConfigureTestDatabase:3.5.16 = org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;4.0.8 = org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase
  @TestEntityManager:3.5.16 = org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;4.0.8 = org.springframework.boot.jpa.test.autoconfigure.TestEntityManager
  @LocalServerPort:3.5.16 = org.springframework.boot.test.web.server.LocalServerPort;4.0.8 = org.springframework.boot.test.web.server.LocalServerPort(没变)
  @SpringBootTest:3.5.16 = org.springframework.boot.test.context.SpringBootTest;4.0.8 = org.springframework.boot.test.context.SpringBootTest(没变)
  @TestConfiguration:3.5.16 = org.springframework.boot.test.context.TestConfiguration;4.0.8 = org.springframework.boot.test.context.TestConfiguration(没变)

14 个注解的新旧全限定名。

模块与 starter

实测 · 本机执行输出
  @WebMvcTest:模块 spring-boot-webmvc-test;带它的 starter:spring-boot-starter-webmvc-test
  @AutoConfigureMockMvc:模块 spring-boot-webmvc-test;带它的 starter:spring-boot-starter-webmvc-test
  @DataJpaTest:模块 spring-boot-data-jpa-test;带它的 starter:spring-boot-starter-data-jpa-test
  @JdbcTest:模块 spring-boot-jdbc-test;带它的 starter:spring-boot-starter-data-jpa-test,spring-boot-starter-jdbc-test
  @AutoConfigureTestDatabase:模块 spring-boot-jdbc-test;带它的 starter:spring-boot-starter-data-jpa-test,spring-boot-starter-jdbc-test
  @JsonTest:模块 spring-boot-test-autoconfigure;spring-boot-starter-test 自带
  @WebFluxTest:模块 spring-boot-webflux-test;带它的 starter:spring-boot-starter-webflux-test
  @DataRedisTest:模块 spring-boot-data-redis-test;带它的 starter:spring-boot-starter-data-redis-test
  @RestClientTest:模块 spring-boot-restclient-test;带它的 starter:spring-boot-starter-restclient-test
  spring-boot-starter-test 自己带的 spring-boot 模块:spring-boot,spring-boot-autoconfigure,spring-boot-starter,spring-boot-starter-logging,spring-boot-starter-test,spring-boot-test,spring-boot-test-autoconfigure

每个切片注解在独立模块里,靠对应的 test starter 引入。

旧 import

实测 · 本机执行输出
  @WebMvcTest + @AutoConfigureMockMvc,用 Boot 3 的包名
    [starter-test] 编译失败:package org.springframework.boot.test.autoconfigure.web.servlet does not exist | cannot find symbol
    [starter-webmvc-test] 编译失败:package org.springframework.boot.test.autoconfigure.web.servlet does not exist | cannot find symbol
    [starter-test-classic] 编译失败:package org.springframework.boot.test.autoconfigure.web.servlet does not exist | cannot find symbol
    [webmvc-test + data-jpa-test] 编译失败:package org.springframework.boot.test.autoconfigure.web.servlet does not exist | cannot find symbol
  @DataJpaTest,用 Boot 3 的包名
    [starter-test] 编译失败:package org.springframework.boot.test.autoconfigure.orm.jpa does not exist | cannot find symbol
    [starter-webmvc-test] 编译失败:package org.springframework.boot.test.autoconfigure.orm.jpa does not exist | cannot find symbol
    [starter-test-classic] 编译失败:package org.springframework.boot.test.autoconfigure.orm.jpa does not exist | cannot find symbol
    [webmvc-test + data-jpa-test] 编译失败:package org.springframework.boot.test.autoconfigure.orm.jpa does not exist | cannot find symbol

四种依赖组合下都编译失败,包括 starter-test-classic。

新 import

实测 · 本机执行输出
  @WebMvcTest + @AutoConfigureMockMvc,新包名
    [starter-test] 编译失败:package org.springframework.boot.webmvc.test.autoconfigure does not exist | cannot find symbol
    [starter-webmvc-test] @WebMvcTest 里 MockMvc 取到 hi  通过
    [starter-test-classic] (没有输出)  [失败: java.io.FileNotFoundException: class path resource [org/springframework/boot/security/autoconfigure/SecurityAutoConfiguration.class] cannot be opened because it does not exist]
    [webmvc-test + data-jpa-test] @WebMvcTest 里 MockMvc 取到 hi  通过
  @DataJpaTest,新包名
    [starter-test] 编译失败:package org.springframework.boot.data.jpa.test.autoconfigure does not exist | cannot find symbol
    [starter-webmvc-test] 编译失败:package org.springframework.boot.data.jpa.test.autoconfigure does not exist | cannot find symbol
    [starter-test-classic] @DataJpaTest 里保存后 count=1  通过
    [webmvc-test + data-jpa-test] @DataJpaTest 里保存后 count=1  通过

starter-webmvc-test、data-jpa-test 下通过;classic 下 @WebMvcTest 运行时失败。

分步演示

测试切片
本步的对象

    本文用到的工具与边界

    • 本机 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 与 3.5.16;注解的全限定名取自两个版本类路径上 spring-boot 系列 jar 里的类;4.0.8 的类路径用 spring-boot-starter-test-classic 解析;各 starter 带的模块取自 Maven 解析出的类路径;编译与 JUnit Platform Console Launcher 运行均在独立 JVM 中进行,@DataJpaTest 使用 H2)
    • 没有验证:@WebFluxTest、@DataRedisTest、@RestClientTest、@JdbcTest 的实际运行(只确认了全限定名与所在模块);starter-test-classic 下 @WebMvcTest 失败的原因(只记录了现象,它带上了 spring-boot-security-test);@MockMvc 与 MockMvcTester 的差别;Boot 4 官方迁移指南对这些包名的说明;IDE 的自动导入能否识别新包名
    京ICP备2026067502号-1京公网安备11010502063487号