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

}
相关推荐
paopaokaka_luck13 分钟前
基于Springboot3+Vue3的宠物殡葬管理系统(webSocket实时通讯、接入腾讯地图、支付宝沙盒支付、Echarts图形化分析)
java·开发语言·网络·后端
卷无止境17 分钟前
Python 中基于 Qt 的 GUI 库授权方式全解析
后端·python
程序员爱钓鱼17 分钟前
Rust 注释与文档注释详解:从代码说明到 cargo doc
后端·rust
前鼻音太阳熊26 分钟前
【MES系统】MES为什么需要SSE?从设备实时监控谈Spring Boot流式推送设计
java·spring boot·后端
卷无止境30 分钟前
Guardrails.ai:为大语言模型加一道"安全阀"
后端·python
尚早立志1 小时前
六)Spring Boot 源码研读之prepareContext 准备上下文
java·spring boot·后端·spring
Dovis(誓平步青云)2 小时前
《Linux CPU频率为何忽高忽低:cpufreq、idle状态与性能抖动教程》
linux·运维·服务器·spring boot·后端·生成对抗网络
To_OC9 小时前
手写 AI 编程 Agent 的命令执行工具:我被 child_process 坑出来的实战经验
后端·node.js·agent
逝水无殇11 小时前
C# 异常处理详解
开发语言·后端·c#
考虑考虑14 小时前
Sentinel安装
java·后端·微服务