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

}
相关推荐
进击的程序猿~33 分钟前
Go Slice源码深度解析指南
开发语言·后端·golang
程序员爱钓鱼44 分钟前
Rust 变量与不可变性:为什么默认不能修改变量?
前端·后端·rust
GoGeekBaird9 小时前
我开源了 BeeWeave,给 AI Agent 搭一个越用越懂你的知识创作台
后端·github
西门吹-禅11 小时前
java springboot N+1问题
java·开发语言·spring boot
weixin_BYSJ198712 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php
犀利豆12 小时前
AI in Harness(四)
人工智能·后端
Hui Baby12 小时前
Spring Security
java·后端·spring
leoZ23113 小时前
Claude 驱动的全栈开发:Spring Boot + Vue 的 BS 架构实践
vue.js·spring boot·架构
逝水无殇13 小时前
C# 运算符重载详解
开发语言·后端·c#
苏三说技术14 小时前
2026编程圈很火的10个Skills
后端