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

}
相关推荐
皮皮林5517 小时前
代码越“整洁”,性能越“拉胯”?
后端
金金金__8 小时前
一篇文章带你入门OpenSpec
后端
阿维的博客日记8 小时前
MultipartFile 是不是表示仅仅是一个分片?
java·后端·spring·multipartfile
海上彼尚8 小时前
Nodejs也能写Agent - 16.LangGraph篇 - 条件分支与循环
前端·后端·langchain·node.js
Dovis(誓平步青云)10 小时前
远程办公软件文件传输实测:6 款工具的速度、稳定性和办公体验对比
linux·运维·服务器·后端·生成对抗网络
凌虚11 小时前
AI 时代,程序员会消失吗?
人工智能·后端·程序员
用户83562907805111 小时前
使用 Python 在 Excel 中添加和自定义文本框
后端·python
乐观的Terry12 小时前
3、数据库设计与领域实体
java·数据库·spring boot·spring cloud·ai编程
干到60岁退休的码农12 小时前
SpringMVC实现Restful api请求方法
后端·restful