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

}
相关推荐
凌览5 分钟前
为什么我不推荐一人公司用PostgreSQL
前端·后端·node.js
亦暖筑序7 分钟前
Spring AI多模型路由实战:企业级智能路由+自动降本指南
spring boot·大模型·企业开发·spring ai·多模型路由
我是一颗柠檬12 分钟前
【Java后端技术亮点】Feed流三级缓存设计,从10秒到100毫秒的优化实战
java·开发语言·后端·缓存
JaguarJack16 分钟前
PHP 应用 security.txt 漏洞披露实践
后端·php
程序员三明治19 分钟前
【AI】RAG 数据分块(Chunk)策略与实践
java·人工智能·后端·ai·大模型·llm·rag
RemainderTime1 小时前
Spring Boot脚手架集成Sa-Token实现生产级RBAC权限管理
java·spring boot·后端·系统架构
世界尽头与你1 小时前
Spring Boot Watcher 未授权访问漏洞
spring boot·安全·网络安全·渗透测试
lpd_lt1 小时前
AI生成Spring Boot + Vue 3 + MySQL + MyBatis-Plus的项目实战
java·spring boot·vue·ai编程
绝知此事1 小时前
RabbitMQ 从入门到精通:Spring Boot 实战三部曲(三)—— 高级应用与性能优化
spring boot·rabbitmq·java-rabbitmq
绝知此事1 小时前
RabbitMQ 从入门到精通:Spring Boot 实战三部曲(一)—— 基础核心与快速上手
spring boot·rabbitmq·java-rabbitmq