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

}
相关推荐
明月_清风32 分钟前
开发者网络概念全扫盲:一篇搞定
后端·网络协议
明月_清风38 分钟前
零信任入门:从"城堡护城河"到"每次进门都要刷卡"
后端
站大爷IP2 小时前
Python循环中修改字典键导致遍历异常深度解析实战案例
后端
掘金者阿豪5 小时前
高可用读写分离实战(二):我把数据库主库停了,结果整个集群的反应和我想象的不一样
后端
掘金者阿豪5 小时前
《高可用读写分离集群实战》系列(一)
后端
Dilee5 小时前
Spring AI 2.0.0 Prompt 最小 Demo:system、user、template 到底怎么分工
后端
未秃头的程序猿5 小时前
Java 26正式发布!这3个新特性,让代码量直接减半
java·后端·面试
小旭Coding5 小时前
卧靠!Go 传给前端的 int64 竟然变成了这个?
后端