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

}
相关推荐
打工仔折腾 AI7 小时前
工业场景下时序库与实时计算一体化架构选型实践对比
java·开发语言·后端·python·性能优化·架构·ai agent 实战
蜗牛互联网8 小时前
长任务多Agent共享文件系统的Manifest交接模式
java·人工智能·后端
苏supper9 小时前
Spring Boot多环境配置与打包运行
spring boot·后端·spring
geovindu10 小时前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
SearchMan10 小时前
还有人不知道&和&&的区别?
后端
想用offer打牌10 小时前
Personal Agent爆火 - 它到底是个什么
人工智能·后端·ai编程
IT_陈寒10 小时前
Vue的v-if和v-for混用居然是个天坑
前端·人工智能·后端
天衍四九-10 小时前
【无标题】
前端·spring boot·mysql·nginx·docker
今年下半年11 小时前
【Spring Boot】多种存储文件(FastDFS / MinIO / 阿里云 OSS)接入设计说明(附源码)
spring boot·分布式·中间件·简单工厂模式·策略模式
白山编程大哥12 小时前
Lua 中使用 C 语言的用户自定义类型——userdata
c语言·junit·lua