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

}
相关推荐
小满zs7 小时前
Go语言第十七章(反射)
后端·go
小满zs7 小时前
Go语言第十五章(JSON序列化)
后端·go
小满zs7 小时前
Go语言第十六章(自定义类型)
后端·go
yz_aiks8 小时前
Spring AI 2.1 ChatMemory 已过时用什么替代:迁移到 Session Memory 实战(JDBC 存储与会话压缩)
java·spring boot·大模型·ai agent·spring ai
金銀銅鐵8 小时前
[Java] 用 GUI 展示 EnumMap 增删键值对时的内部变化
java·后端·ai编程
学长毕设8 小时前
基于SpringBoot的上门洗车服务系统设计与实现
java·spring boot·后端
小溪学编程12 小时前
Spring @Bean 与 @Component 注解的区别详解
java·后端·spring
IT_陈寒12 小时前
Python的列表推导式用不好真的会爆内存你知道吗
前端·人工智能·后端
摸爬滚打的小李13 小时前
TomCat
后端
itmigrate14 小时前
Rust 里的所有权误区:不是不写 clone 就叫高性能
开发语言·后端·rust