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

}
相关推荐
程序员爱钓鱼20 小时前
Rust 可变借用详解:&mut T 与安全修改数据
后端·面试·rust
程序员爱钓鱼20 小时前
Go 类型转换详解
后端·面试·go
mldong1 天前
工作流引擎的灵魂:状态机与 submitType
后端
朦胧之1 天前
Go 基础
后端·go
lun1 天前
Agent 工具调用成长史:理清楚 Function Calling、MCP、CLI 的关系
后端
Python私教1 天前
Codex 自动写 Commit 够安全吗?一套证据化 Git 提交流程
人工智能·git·后端
Python私教1 天前
AI Agent 上生产要不要开写权限?我把执行链拆成 4 道闸门
人工智能·后端·python
lun1 天前
Claude Code 多 Agent 实现:subAgent & Agent Teams
后端
SomeB1oody1 天前
【RustyML入门】2.9. MeanShift
开发语言·后端·机器学习·rust·教程
weixin_BYSJ19871 天前
springboot酒店管理系统--附源码27436
java·spring boot·python·随机森林·贪心算法·eclipse·django