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

}
相关推荐
yume_sibai18 小时前
03-Rust 函数式编程特性(闭包 + Iterator + Option/Result + 链式调用)
开发语言·后端·rust
吃饱了得干活18 小时前
Redis 不是死脑筋,它是一套“会进化”的存储系统
redis·后端
伩仁18 小时前
别再 HTTP 200 一把梭了:用 RFC 9457 Problem Details 给 FastAPI 错误响应"立规矩"
后端
MeetTanG18 小时前
Go 实战锦囊|errgroup:优雅地管理并发任务组
后端·go
xiaoqiMikko18 小时前
七条 advisory 各给了一个修复版,而一次修完的那个数字一条都没写
java·spring boot
PragmaticWorks18 小时前
DDD 学了很多却用不上?因为你把“责任”和“时机”揉在了一起
后端·领域驱动设计
凯哥Java18 小时前
System.setProperty 的正确姿势:Spring Boot 启动类里的“缺省值“魔法
java·spring boot·后端
柯克七七18 小时前
Vue刷新页面数据重置?一招解决Vuex状态丢失
后端
楚兴19 小时前
ACP 到底解决了什么?让 IDE 和 Coding Agent 解耦
人工智能·后端·架构
楚兴19 小时前
DeepSeek Harness 到底在做什么?拆开 Agent 的运行时
人工智能·后端·架构