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

}
相关推荐
卷无止境20 小时前
LiteLLM 全面解析开源AI网关如何统一管理百余种大模型
后端·python
__zRainy__20 小时前
Node系列 · Node基础:https 模块
后端·网络协议·http·https·node.js
AINative软件工程21 小时前
LLM Token Budget 工程实践:给每个请求设上限,让成本和质量都在掌控中
后端·llm·ai编程
༄沐࿆风࿆࿆21 小时前
Spring Boot 3 + Vue 3 全栈外卖系统:RabbitMQ异步解耦、MySQL/Neo4j双引擎推建
vue.js·spring boot·java-rabbitmq
卷无止境21 小时前
终端里的AI辅助,一场正在发生的编程效率变革
后端·python
程序员爱钓鱼21 小时前
Go 编程实战:Map——使用 Key-Value 管理键值数据
后端·rust·go
程序员爱钓鱼1 天前
Rust Trait Object详解:dyn Trait与动态分发
后端·面试·rust
To_OC1 天前
Next.js + Redis 构建笔记系统:Redis Hash 实战与性能优化
redis·后端·next.js
北斗落凡尘1 天前
LangGraph 入门实战(11)--输出模式
后端·python·langchain
demo007x1 天前
Hermes-Agent 技术架构
前端·后端·agent