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

}
相关推荐
掘金者阿豪12 分钟前
Node.js 连接金仓数据库踩坑记(上篇):环境搭建与基础操作
后端
肌肉娃子1 小时前
20260603.记一次 Doris FE “幽灵卡死”引发的惨案:从表象到真凶的追凶实录
后端
学以智用1 小时前
.NET Core 序列化 **超清晰完整版教程**
后端·.net
Java患者·1 小时前
Spring Boot 3 整合 Elasticsearch 8
spring boot·后端·elasticsearch
雪隐1 小时前
个人电脑玩AI01-让5060 Ti给你打工——Whisper语音识别篇(上)
人工智能·后端
我是一颗柠檬2 小时前
【Redis】主从复制Day9
java·数据库·redis·后端
Wenzar_2 小时前
GeoHash+Redis Streams实时围栏系统实战
java·数据库·redis·junit
侯盛鑫2 小时前
理解 RocksDB IngestExternalFile
数据库·后端