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);
    }

}
相关推荐
MC皮蛋侠客7 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
李昊哲小课8 小时前
SpringBoot4 云端咖啡站 阶段二:数据访问与分层架构
spring boot·架构
Profile排查笔记9 小时前
指纹浏览器怎么设置 IP?代理配置、检测与排错流程
前端·人工智能·后端·自动化
宠友信息10 小时前
社区类源码开发实践中的仿小红书系统技术要点分析
java·spring boot·redis·mysql·uni-app·vue·内容运营
IT_陈寒10 小时前
SpringBoot自动配置失效?你可能漏了这个小开关
前端·人工智能·后端
摇滚侠10 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发应用 阅读笔记 1
spring boot·笔记·后端
用户83562907805110 小时前
使用 Python 在 Excel 中插入 OLE 对象
后端·python
程序员小八77711 小时前
Go 语言特性
开发语言·后端·golang
XuCoder11 小时前
你以为隔离级别只是个开关,可 MySQL 凭什么靠它挡住幻读?
后端
创新技术阁11 小时前
FastapiAdmin 前后端启动全流程详解
前端·后端·fastapi