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

}
相关推荐
HySpark19 小时前
语音输入法实践:Rust 集成 Paraformer 推理引擎踩坑与优化过程
人工智能·后端·语音识别·熙瑾会悟
郡杰19 小时前
yudao-cloud及测试
后端
卷无止境19 小时前
Python 装饰器:给函数穿件"外套",到底难在哪?
后端·python
深爱水瓶 血影S狂风19 小时前
Asp.Net无刷新上传并裁剪头像
后端·asp.net
用户81818706274620 小时前
第30章 Web 层与事务异常:参数校验、@Transactional 失效场景全集
后端
Java内核笔记20 小时前
BeanRegistrar:Spring Boot 4 最被低估的新特性,彻底改变 Bean 注册方式
java·后端
还是鼠鼠20 小时前
【Spring AI智能体实战 02】Spring Boot 3.4整合Spring AI:项目创建与依赖配置
java·人工智能·spring boot·maven·spring ai
用户81818706274620 小时前
第28章 RPC 框架异常:Dubbo / gRPC
后端
搬砖小匠20 小时前
SpringBoot 通过自定义注解 + AOP 实现数据字典自动翻译(通用方案)
java·后端