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

}
相关推荐
codeGoogle5 分钟前
2026 年 IM 怎么选?聊聊 4 家主流即时通讯方案的差异
android·前端·后端
小沈同学呀6 分钟前
Spring Boot实现加密字段模糊查询的最佳实践
java·spring boot·后端·encrypt
Maguyusi7 分钟前
go 批量生成 c++与lua的proto文件
开发语言·后端·golang·protobuf
万能的小裴同学8 分钟前
饥荒Mod
java·开发语言·junit
J_liaty14 分钟前
SpringBoot缓存预热:ApplicationRunner与CommandLineRunner深度对比与实战
spring boot·后端·缓存
BingoGo18 分钟前
如何重构遗留 PHP 代码 不至于崩溃
后端·php
康小庄19 分钟前
Java自旋锁与读写锁
java·开发语言·spring boot·python·spring·intellij-idea
沙河板混20 分钟前
@Mapper注解和@MapperScan注解
java·spring boot·spring
墨染青竹梦悠然22 分钟前
基于Django+vue的单词学习平台
前端·vue.js·后端·python·django·毕业设计·毕设
Victor35623 分钟前
MongoDB(11)MongoDB的默认端口号是多少?
后端