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

}
相关推荐
开心猴爷7 小时前
应对iOS审核4.3:可以使用 IpaGuard 来给属性名混淆的技术细节与平衡之道
后端·ios
1m555555557 小时前
RAG 工程实战(五):正确知识已经进入 Prompt,为什么大模型还是会答错?
后端·面试
易小尊7 小时前
我的模型在 65% 以上时准得可怕,在 50% 时像在掷硬币
后端
用户2788607285677 小时前
fallback 会盖掉平台提示词:system、空数组和 systemAppend 的差别
前端·后端
iOS开发上架哦7 小时前
iOS应用耗电测试:使用Energy Log、Keymob和Instruments进行性能监控与能源消耗分析
后端·ios
掘金挖土7 小时前
前端手摸手跑路之 AI 应用开发(十一)
前端·后端
用户9888321871747 小时前
从“限流数字”到可维护策略:ASP.NET Core 固定窗口限流的落地边界
后端
IT_陈寒7 小时前
Vue动态组件缓存的坑你踩过了吗?
前端·人工智能·后端
顽疲7 小时前
java springboot vue 养老系统源码详解:请假 care_leave 销假与床位停费日扣实践
java·vue.js·spring boot
雾行0x008 小时前
粗排:如何在毫秒级筛出值得精排的内容?
后端·架构