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

}
相关推荐
敲代码的嘎仔12 分钟前
从零搭建微服务:Spring Cloud Alibaba 全家桶踩坑实录(Nacos + OpenFeign + Gateway + Sentinel)
java·spring boot·spring·spring cloud·微服务·gateway·sentinel
程序员爱钓鱼21 分钟前
Rust 所有权 Ownership 详解:理解内存安全的核心机制
前端·后端·rust
JaguarJack29 分钟前
PHP 引用计数机制深度解析
后端·php·服务端
一条泥憨鱼1 小时前
苍穹外卖【day7| 对购物车进行操作】
java·后端·mysql·苍穹外卖
陈随易9 小时前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级
前端·后端·程序员
IT界的老黄牛11 小时前
Spring Boot 的 `/actuator/health` 不是免费的:健康检查打爆连接池的反面教材
spring boot·hikaricp·actuator·健康检查·线上排障·架构反模式
凌虚12 小时前
面向 MySQL 用户的 PostgreSQL 快速上手指南
数据库·后端·架构
IT_陈寒13 小时前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端
前端开发张小七14 小时前
Java 学习笔记 · 第一课:基础语法与核心概念
java·后端
海盗123414 小时前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore