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

}
相关推荐
遇见你...4 小时前
A01-Spring概述
java·后端·spring
代码匠心4 小时前
从零开始学Flink:TopN 榜单
大数据·后端·flink·flink sql·大数据处理
lizhongxuan6 小时前
Claude Code 防上下文爆炸:源码级深度解析
前端·后端
Warson_L7 小时前
Python 流程控制与逻辑
后端·python
糖炒栗子03267 小时前
架构笔记:应用配置无状态化 (Statelessness)
后端
Warson_L7 小时前
Python 四大组合数据类型 (Collection Types)
后端·python
查古穆7 小时前
大白话讲ReAct:大模型的“边想边干”
后端
于先生吖7 小时前
SpringBoot+MQTT 无人健身房智能管控系统源码实战
java·spring boot·后端
毕设源码-小云学姐7 小时前
计算机毕业设计springboot网上招聘系统 基于SpringBoot的在线人才对接平台设计与实现 SpringBoot框架下的数字化求职招聘服务系统开发
spring boot·后端·课程设计
weyyhdke10 小时前
springboot和springframework版本依赖关系
java·spring boot·后端