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

}
相关推荐
copyer_xyf12 小时前
Python 内存分析:从栈和堆理解对象引用
前端·后端·python
可乐ea13 小时前
【知识获取与分享社区项目 | 项目日记第 19 天】基于 Elasticsearch 实现关键词检索与业务权重排序
java·大数据·spring boot·mysql·elasticsearch·搜索引擎·全文检索
金銀銅鐵13 小时前
用 Tkinter 实现一个简单的罗马数字转化工具
后端·python
RemainderTime14 小时前
Spring Boot脚手架集成 Spring Security实现生产级RBAC鉴权
spring boot·后端·spring
宸津-代码粉碎机14 小时前
Spring AI企业级Agent实战|多工具自动规划+并行调度落地,彻底解决复杂业务AI任务编排问题
java·大数据·人工智能·spring boot·python·spring
biubiubiu070614 小时前
自定义starter 可以导入SpringBoot直接使用
java·spring boot·spring
用户23307130747914 小时前
对象的一生(上)
后端
爱勇宝14 小时前
如何评估 AI 大模型的商业价值?
前端·后端·程序员
小刘|14 小时前
SpringBoot整合LangChain4j实现流式AI对话
java·spring boot·langchain
AskHarries14 小时前
Landing Page 验证法
后端