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

}
相关推荐
nnerddboy13 小时前
Rust教程04:引用、借用与切片
开发语言·后端·rust
Rain的Java大神之路13 小时前
线上接口负载满了如何解决
java·数据库·redis·后端·缓存·面试·架构
weixin_4316004414 小时前
NestJS 入门(7):生命周期钩子——构造函数和 `OnModuleInit` 差在哪?
前端·后端·学习·node.js·nest.js
小强库计算机毕业设计14 小时前
基于 Spring Boot + Vue3 的校园管理系统
java·spring boot·后端·项目实战·管理系统·技术分享·校园管理系统实战
canonical_entropy15 小时前
关于《Mission Driver:Loop Engineering 的一种通用参考实现》的补充说明
后端·agent·ai编程
__zRainy__15 小时前
Node系列 · Node基础:Node.js 概述
后端·node.js
星火102416 小时前
【LangChain4j系列06】ChatMemory 对话记忆管理
人工智能·后端
netCode16 小时前
Git一文带你理解如何使用
后端
睡觉时不困44216 小时前
12.LangChain 1.0+ 第二篇:Prompt、结构化输出与 LCEL 组件组合.发布清单
后端
SomeB1oody16 小时前
【RustyML入门】2.13. 孤立森林
开发语言·后端·机器学习·rust·教程