You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.7.0中无法找到org.junit.Test注解问题咨询

问题原因

Spring Boot 2.7.0 版本对测试组件做了调整,spring-boot-starter-test不再默认集成JUnit 4相关依赖,仅内置JUnit 5(Jupiter)作为默认测试框架。
你找不到的org.junit.Test是JUnit 4提供的注解,代码补全时弹出的org.junit.jupiter.api.Test是JUnit 5的原生注解,属于版本迭代后的正常现象,不是Eclipse配置错误或者依赖加载失败。

触发代码补全时的实际效果:
Eclipse中@Test注解补全仅显示JUnit5选项

解决方法

根据自己的开发需求选一种方案即可:

直接使用JUnit 5(官方推荐)

不需要修改现有pom.xml配置,直接导入org.junit.jupiter.api.Test注解编写测试用例即可,这是Spring Boot 2.7+版本官方主推的测试方案,和框架的兼容性最好。

JUnit 5和JUnit 4的常用注解存在命名差异,迁移时可以参考对应关系:

  • JUnit 4 @Before → JUnit 5 @BeforeEach
  • JUnit 4 @After → JUnit 5 @AfterEach
  • JUnit 4 @BeforeClass → JUnit 5 @BeforeAll
  • JUnit 4 @AfterClass → JUnit 5 @AfterAll

兼容原有JUnit 4写法

如果项目里有大量存量JUnit 4测试代码不想重构,只需要在pom.xml的<dependencies>节点下手动添加JUnit Vintage引擎依赖,就能继续使用org.junit.Test注解:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>

添加依赖后,在Eclipse中右键项目,依次选择Maven > Update Project,等待依赖下载、项目刷新完成后,再触发代码补全就能看到org.junit.Test的导入选项了。


内容的提问来源于stack exchange,提问作者Rishiraj Fasalkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 21:30:42