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

}
相关推荐
小白君6536 小时前
互联网大厂Java面试:从Spring Boot到微服务的技术场景深度解析
spring boot·redis·微服务·消息队列·java面试·数据库优化
Shadow(⊙o⊙)6 小时前
初识Qt+经典方式实现hello world!的交互
开发语言·c++·后端·qt·学习
imuliuliang6 小时前
五大编程语言核心对比:特性与应用全解析
运维·spring boot·nginx
Neobee7 小时前
国内用 Terraform 管 Cloudflare 踩过的 5 个坑(附可直接复用的代码)
后端
平凡但不平庸的码农7 小时前
Go context 包详解
开发语言·后端·golang
Gopher_HBo7 小时前
分布式详解
后端
AI人工智能+电脑小能手7 小时前
【大白话说Java面试题】【Java基础篇】第38题:两个对象的hashCode()相同,则 equals()是否也一定为 true?
java·开发语言·后端·面试·hash-index
ahauedu7 小时前
流水账债务
spring boot
SamDeepThinking7 小时前
所有的框架源码,最怕的就是被debug
java·后端·程序员