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

}
相关推荐
覆东流4 分钟前
第11天:python字典基础
开发语言·后端·python
IT_陈寒26 分钟前
JavaScript实战技巧总结
前端·人工智能·后端
_Evan_Yao29 分钟前
return 的迷途:try-catch-finally 中 return 的诡异顺序与 Spring 事务暗坑
java·后端·spring·mybatis
薛定谔的猫喵喵30 分钟前
Spring Boot Jar包修改配置文件和Class中硬编码IP的完整指南
java·spring boot·反编译·class
海兰40 分钟前
【第35篇】文本摘要微服务
人工智能·spring boot·微服务·架构·spring ai
未来龙皇小蓝41 分钟前
SpringBoot API日志系统设计-02:线程池异步化与RabbitMQ解耦
数据库·spring boot·后端·性能优化·rabbitmq·java-rabbitmq
小杍随笔1 小时前
【Tauri 2 + Rust 配置 WebView2 缓存数据存储到安装目录】
开发语言·后端·rust·tauri
楼田莉子1 小时前
仿Muduo的高并发服务器:基于Tcp协议的回显服务器
linux·服务器·c++·后端
咖啡八杯2 小时前
GoF设计模式——工厂方法模式
java·后端·设计模式