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

}
相关推荐
星浩AI32 分钟前
OpenHuman 对比 OpenClaw、Hermes Agent
人工智能·后端·agent
小江的记录本38 分钟前
【Java基础】泛型:泛型擦除、通配符、上下界限定(附《思维导图》+《面试高频考点清单》)
java·数据结构·后端·mysql·spring·面试·职场和发展
geovindu3 小时前
go: Semaphore Pattern
开发语言·后端·设计模式·golang·企业级信号量模式
IT_陈寒3 小时前
Redis内存用爆了,原来我们都忽略了这个配置
前端·人工智能·后端
武子康3 小时前
Java-02 深入浅出MyBatis 3 快速入门:环境配置、项目创建与 CRUD 操作
java·后端
happymaker06263 小时前
SpringBoot学习日记——DAY02(SpringBoot整合Swagger3)
java·spring boot·学习
未若君雅裁4 小时前
Spring Boot 自动配置原理与常用注解
java·spring boot·后端
Xiacqi14 小时前
Java数据库连接--JDBC--DRUID
数据库·后端
浮游本尊4 小时前
用结构化 Prompt 让大模型「干活」:以数据库巡检告警建议生成为例
后端
snakeshe10104 小时前
SpringBoot 多人协作平台实战(8):Cookie 与登录状态维持
后端