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 小时前
其实跨域问题是后端来解决的? CORS
后端·面试·go
掘金一周16 小时前
掘友们,一人说一个你买过夯到爆的东西 | 沸点周刊 4.23
前端·人工智能·后端
Developer_Niuge16 小时前
告别翻不动的 1000+ 书签:开源 Chrome / Edge 浏览器书签管理插件 Smart Bookmark 0.2 发布
前端·后端
ElevenPlus16 小时前
【 AI智能体时代:一名Javaer的技术随想录】MCP服务部署架构
后端
千寻girling16 小时前
RabbitMQ 详细教程(38K字数)
java·后端·面试
Rust研习社16 小时前
Rust 多线程从入门到实战
开发语言·后端·rust
卷毛的技术笔记16 小时前
从“拆东墙补西墙”到“最终一致”:分布式事务在Spring Boot/Cloud中的破局之道
java·spring boot·分布式·后端·spring cloud·面试·rocketmq
袋鱼不重17 小时前
Hermes Agent 直连飞书机器人
前端·后端·ai编程
Pkmer17 小时前
古法编程: 深度解析Java调度器Timer
java·后端
小强198817 小时前
C++23/26新特性解析:那些让你放弃Boost库的杀手锏
后端