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

}
相关推荐
苏三说技术8 小时前
为什么越来越多人使用FastAPI?
后端
老孙讲技术8 小时前
业主半夜想看楼道监控,物业却说「去机房」?我用设备托管+轻应用,把小区摄像头嵌进了社区小程序
后端·物联网
geovindu8 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
二月龙8 小时前
什么是事务四大特性?用业务案例通俗讲透 ACID
后端
Slice_cy9 小时前
Mint 自研框架设计与实现:从重复开发走向配置驱动(五)
前端·后端·架构
景同学9 小时前
把 AI 用到线上运维:可行、有效,前提是喂足信息——一次 Full GC 排障实录
java·人工智能·后端
神奇小汤圆9 小时前
MyBatis、MyBatis-Plus、通用 Mapper:一张图说清三者的血缘关系
后端
SamDeepThinking9 小时前
微信支付对接实战:从下单到回调的完整落地过程
后端·程序员·架构
神奇小汤圆10 小时前
架构师必备:分布式锁方案选型
后端