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

}
相关推荐
SimonKing15 小时前
铁子,IntelliJ IDEA 2026.1.3来了,升不升?
java·后端·程序员
铁皮饭盒15 小时前
@kognitivedev/rag, 用js做AI Agent开发
javascript·后端
IT_陈寒15 小时前
JavaScript的默认参数挖坑实录,我掉进去了
前端·人工智能·后端
陈明勇16 小时前
Go 1.26 新特性回顾:语言增强、工具升级与 Green Tea GC 默认启用
后端·go
咖啡八杯1 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
lizhongxuan1 天前
AI Agent 上下文压缩利器 Headroom
后端
Csvn1 天前
SSH 远程管理与安全加固 — 运维的守门之道
后端
IT_陈寒1 天前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
菜鸟谢1 天前
Rust 智能指针完整详解
后端
java小白小1 天前
SpringBoot(01): 初识SpringBoot,从Spring的痛点说起
spring boot