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

}
相关推荐
苍何15 小时前
高考填志愿,我做了个 Skill,300 个 Agent 同时查公司
后端
yspwf15 小时前
NestJS 配置管理完整方案
后端·架构·node.js
雪隐16 小时前
个人电脑玩AI-03让5060 Ti给你打工——paddleOCR
人工智能·后端
AskHarries16 小时前
Shell Tool:命令执行、输出读取和长任务管理
后端
苍何16 小时前
开源项目想出海,我让 AI 员工帮我找海外达人
后端
java1234_小锋16 小时前
请描述 Spring Boot 的启动流程,包括 SpringApplication 的初始化和 run 方法的核心步骤。
java·数据库·spring boot
长栎16 小时前
你在 Controller 里注入 8 个 Service,其实是想请一个中介者
后端
小闹54916 小时前
Docker 如何才能学的更扎实
后端·程序员
武子康16 小时前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis