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

}
相关推荐
云浪36 分钟前
Go 并发实战:从零实现一个高并发安全的任务调度器
javascript·后端·go
苏三的开发日记1 小时前
Docker 启动顺序优化(CentOS / Ubuntu)
后端
高频因子挖掘机3 小时前
同一交易日出现重复 K 线:去重前先确认时间、来源和粒度
后端·github·api
feely02083 小时前
《我写了 31 个静态扫描插件,踩了 7 个「假绿」》
后端
橙子家4 小时前
参数校验类异常(Argument Exception)【C# Exception 详解系列一】
后端
掘金挖土4 小时前
前端手摸手跑路之 AI 应用开发(九)
前端·后端
全栈弄潮儿5 小时前
Python实战第8期:函数
后端·python·编程语言
码事漫谈5 小时前
你让 AI “尽力做”,它反而可能做不好
后端
孟健6 小时前
LLM API 安全防线:从 OpenCode 盗刷事件到 LiteLLM 多模型容灾实操
人工智能·后端·架构