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

}
相关推荐
Csvn5 小时前
Nginx 静态站点与 location 匹配:优先级、alias、缓存头与 gzip
后端·nginx
小满zs6 小时前
Go语言第二十章(初探Gin)
后端·go
学长毕设6 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
小满zs6 小时前
Go语言第十九章(http标准库)
后端·go
考虑考虑8 小时前
nginx拦截url
运维·后端·nginx
GISer_Jing8 小时前
GitHub热点I
后端·ai·langchain
谭光志9 小时前
如何让 AI 高效工作
前端·后端·ai编程
ServBay9 小时前
连AI自己也不愿意维护自己的屎山代码
后端·ai编程·claude
code2cat10 小时前
【随笔】MCP长任务:查询已经返回,为什么任务还没有完成
javascript·人工智能·后端·mcp
Java与AI落地10 小时前
别再正则抠 JSON 了:用 Spring AI 的 entity() 把模型输出变成对象
后端