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

}
相关推荐
小荷才露尖尖角,早有蜻蜓立上头13 小时前
SpringBoot 定时任务
java·spring boot·后端
ServBay13 小时前
AI 请求去哪了?OpenRouter 路由、供应商回退与数据留存
后端·aigc·ai编程
Moment14 小时前
AI 写代码越来越快,程序员为什么反而更累了
前端·后端·面试
apeman14 小时前
git-cherry合并代码工具
前端·后端
行百里er15 小时前
轻量级 Spring 监测工具 Spring Insight 版本进展:Server 2.1.0 + Agent 0.6.0
后端·开源·监控
夜深忽梦少年事G16 小时前
MySQL 排序解析:索引有序扫描、filesort、优先队列
后端
why技术17 小时前
我把这套 skills 引入团队,跑了好几个月后,说点真实体感。
前端·后端·ai编程
zhoulcc18 小时前
前端响应处理:AjaxResult与TableDataInfo的JSON结构区别及统一取值方案
前端·后端·bug·ajaxresult·tabledatainfo
geovindu18 小时前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
geovindu19 小时前
python:Lasso
开发语言·后端·python·算法