Spring Boot 测试用例必须要加 @RunWith 吗

default

JUnit 4(必须显式添加 @RunWith)

1
2
3
4
5
6
@RunWith(SpringRunner.class) // 关键:启用 Spring 支持
@SpringBootTest
public class MyTest {
    @Autowired
    private MyService service; // 依赖注入需要 Spring 上下文
}

JUnit 5(隐式支持,无需显式扩展)

1
2
3
4
5
@SpringBootTest // 在 JUnit 5 中默认包含 SpringExtension
public class MyTest {
    @Autowired
    private MyService service;
}
Comments
  • Latest
  • Oldest
  • Hottest
No comment yet.
Powered by Waline v2.15.8
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……