Develop 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 上下文 } CopyJUnit 5(隐式支持,无需显式扩展) 1 2 3 4 5 @SpringBootTest // 在 JUnit 5 中默认包含 SpringExtension public class MyTest { @Autowired private MyService service; } Copy
Preview: