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

}
相关推荐
geovindu8 小时前
python:DecisionTree
开发语言·后端·python·算法
Cosolar8 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
geovindu9 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
码事漫谈10 小时前
黄仁勋的来时路
后端
bai27310 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
hackhase10 小时前
Django 多表数据整合:7 张异构表如何收敛成 4 类页面
后端·mysql
Cosolar12 小时前
需求做完了,经验归零了:Agent 真正缺的是第二条闭环
人工智能·后端·agent
CRZZX14 小时前
Agent 上下文管理与可观测性设计
后端·agent
yAdrian14 小时前
将 milkv-duo-s 开发板变成 AP
后端·嵌入式