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

}
相关推荐
努力的小雨3 小时前
手里捏着纸,谁来暂停视频?我把折纸老师塞进了 AI 眼镜
后端
狗头大军之江苏分军3 小时前
《潮水漫过十七岁》倒计时
前端·javascript·后端
Wellijohn4 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
HarmonSir5 小时前
OpenManus 的架构解析:另一个通用 Agent 的最小内核与扩展边界
后端·架构
hsfxuebao5 小时前
Sandbox(沙箱)到底是什么?
后端
一条Go5 小时前
proto 契约:校验注解、错误码与「文案即接口」
后端·github
狗头大军之江苏分军5 小时前
《潮水漫过十七岁》好久没写了
前端·javascript·后端
苍何6 小时前
用 OiiOii Motion + Opus 5.5 做动画视频,效果猛的离谱
后端
geovindu6 小时前
python:Logistic Regression
开发语言·后端·python·算法