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

}
相关推荐
明月_清风8 小时前
SaaS 的三层挣钱逻辑,正在被 AI 从第一层击穿
人工智能·后端
谁在黄金彼岸10 小时前
nginx服务化五套方案原理解剖与选型
后端
谁在黄金彼岸10 小时前
WinSW在Win7上失败真相-实测与修复
后端
花间相见10 小时前
【后端开发|Redis进阶01】—— Redis分布式锁全解:从SETNX到Redisson看门狗,再到RedLock
后端·面试
llqbzllll10 小时前
A2A 1.0 到底和 MCP 差在哪:从 Agent Card、Task 到 Java 最小互操作
后端
花间相见10 小时前
【后端开发|JVM基础01】—— Java垃圾回收全解:从分代内存模型到各收集器的分代职责
后端
geovindu10 小时前
rust: Abstract Factory pattern
开发语言·后端·设计模式·rust·抽象工厂模式
pe7er10 小时前
Spring Boot 日志最佳实践:接入、分级、异常记录与滚动拆分
后端
程序员老赵12 小时前
Docker 部署 Dolibarr:轻松搭建开源 ERP/CRM 平台
运维·前端·后端