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

}
相关推荐
starzy19903 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
IT_陈寒3 小时前
Redis那个过期删除策略,差点让我线上崩了
前端·人工智能·后端
ServBay4 小时前
Cursor披露恶意扩展,怎么守住本地 API Key
后端·ai编程·cursor
AndreaYYmm4 小时前
多台设备共享一个 NFS,怎么保证压力测试不同时跑?一把互斥锁的设计与最简验证
后端
心之语歌4 小时前
uni-app uts原生插件开发:uts 与 Kotlin 交互踩坑实录(附解决方案)
前端·后端
AndreaYYmm5 小时前
手动敲能跑,cron 一跑就 command not found?聊聊 PATH 到底从哪来
后端
bcbnb5 小时前
Windows 上 iOS 开发接 CI,KXBuild 自托管 runner 出签名 ipa
后端·ios
Flynt6 小时前
掘金热文说 Spring Boot 4.1 默认开虚拟线程,我实测后发现了更狠的坑
java·spring boot·性能优化
十有八七6 小时前
手搓 Agent(一):先有大脑——大模型只会做一件事
前端·人工智能·后端