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

}
相关推荐
代码丰1 小时前
不依赖 LangGraph,也能实现 Human-in-the-loop:基于数据库状态机的人机协作工作流-CSDN博客
后端·架构
IT_陈寒2 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
Csvn2 小时前
Nginx 与 OpenResty:阶段钩子、shared dict 与动态 upstream/证书
后端·nginx
geovindu3 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
Java与AI落地3 小时前
RAG 第二步:检索质量不行,先别换模型
后端
JavaGuide4 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
喵个咪4 小时前
RushWind Admin — 模块化单体:一个进程与通往微服务的五级阶梯
后端·架构·rust
用户69371750013844 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
Hespethorn4 小时前
文件该放在哪:十个位置、三处边界、三份声明
后端·架构
喵个咪4 小时前
RushWind CMS — 最小微服务实例:微服务架构的最小完备集
后端·架构·rust