SpringBoot整合JUnit

SpringBoot整合JUnit

1.基本要求

要保证测试类所在的目录与上面的包目录对应,这样,在测试的过程中,才能获取到包(com.rql)下注入的Bean和方法。

2.特殊情况

假如测试类不在SpringBoot启动类的包或者子包中,那么需要设定classes:
运行报错,无法进行测试:

java 复制代码
java.lang.IllegalStateException: 
Unable to find a @SpringBootConfiguration,
 you need to use @ContextConfiguration or 
 @SpringBootTest(classes=...) with your test

这个时候,如果还想继续正常测试,可以设定classes = Spring01Application.class

java 复制代码
@SpringBootTest(classes = Spring01Application.class)
class Spring01ApplicationTests {


    @Autowired
    private MyDataSource myDataSource;
    @Test
    void contextLoads() {
        System.out.println(myDataSource);
    }

}
相关推荐
兔子零10242 分钟前
当 Codex 成为主力,软件工程的重心已经变了
前端·后端·架构
用户6757049885028 分钟前
别再死记硬背了!一文扒光 I/O 多路复用的底裤(Epoll/Select/Poll)
后端
牛奶12 分钟前
网关是怎么当"门卫"的?
前端·后端·负载均衡
悟空聊架构20 分钟前
100多G数据同步引发的MySQL集群“连环炸”,我是如何一步步恢复的? - 墨天轮
后端·架构
Hemy0822 分钟前
tauri + rust 创建初始项目
开发语言·后端·rust
锋行天下31 分钟前
后端golang项目一键打包部署方案
后端
用户67570498850232 分钟前
90%的人都不知道:Docker 容器 apt 报错 404 的幕后黑手竟是它!
后端·docker·容器
Apifox.41 分钟前
Apifox 近期更新|AI Agent Debugger、A2A Debugger、Postman API 导入、Ask AI 侧边栏对话
前端·人工智能·后端·测试工具·测试用例·postman