spring-boot-test - 使用配置文件时 SpringBoot 测试无法自动装配服务
问题描述
我试图通过以下方式为我的命令行应用程序运行 SpringBoot 测试
https://www.baeldung.com/spring-junit-prevent-runner-beans-testing-execution
如果我在 Test 类中没有 Autowried,它就可以工作
如果我使用带有 ActiveProfiles 注释的 Autowired 注释,则无法加载服务组件类。
有人可以给我一个提示如何解决它吗?
解决方案
为了解决问题,我们可以在执行主进程之前检查活动配置文件
@Override
public void run(String... args) throws Exception {
if (!Stream.of(env.getActiveProfiles()).collect(Collectors.toCollection(ArrayList::new)).contains("test")) {
myMainProcess();
}
}
推荐阅读
- javascript - 请求 JSON 文件时不执行快速路由
- shell - 将文件拆分为更小的最大 n-chars 文件而不剪切任何行
- dart - 有没有办法在通用 Dart 方法中声明多重继承?
- javascript - 从 ASP.net core 2.1 中的 url 获取参数?
- algorithm - Neo4j 使用什么样的图算法?
- django - 只读文件系统:'/srv/media/house_preview/question_4.PNG'
- mediawiki - 如何获得维基百科用户的所有贡献?
- python - Python 2 从文本文件中排序
- javascript - React Native API - 为什么只使用一个参数进行解构?
- python - Set_Index Inconsistent KeyError