Boot 4 升级后 @WebMvcTest、@DataJpaTest 为什么 package does not exist?新包名是什么,要加哪个 test starter?spring-boot-starter-test-classic 能不能当过渡?这篇在 Spring Boot 4.0.8 上逐个编译、运行。
@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 个注解的新旧全限定名。
@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 引入。
@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。
@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 运行时失败。