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

}
相关推荐
IT_陈寒5 小时前
React Hooks闭包陷阱:你以为的state可能早就过期了
前端·人工智能·后端
小码哥_常5 小时前
细说API:颠覆认知!重新认识RESTful的真正精髓
后端
用户99045017780095 小时前
基于flowable实现在线表单+工作流
后端
苏三说技术5 小时前
Artha已接入MCP,线上问题能用AI排查了!
后端
用户962377954485 小时前
代码审计 | CC2 链 —— _tfactory 赋值问题 PriorityQueue 新入口
后端
爱敲代码的小鱼7 小时前
springboot(2)从基础到项目创建:
java·spring boot·spring
Vfw3VsDKo8 小时前
Maui 实践:Go 接口以类型之名,给 runtime 传递方法参数
开发语言·后端·golang
i220818 Faiz Ul9 小时前
动漫商城|基于springboot + vue动漫商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·动漫商城系统
是真的小外套9 小时前
第十五章:XXE漏洞攻防与其他漏洞全解析
后端·计算机网络·php
ybwycx10 小时前
SpringBoot下获取resources目录下文件的常用方法
java·spring boot·后端