【问题标题】:SpringBoot test cannot Autowire service when profile is use使用配置文件时 SpringBoot 测试无法自动装配服务
【发布时间】:2021-01-10 00:33:53
【问题描述】:

我试图通过以下方式为我的命令行应用程序运行 SpringBoot 测试

https://www.baeldung.com/spring-junit-prevent-runner-beans-testing-execution

如果我在测试类中没有 Autowried,它就可以工作

如果我使用带有 ActiveProfiles 注释的 Autowired 注释,它无法加载服务组件类。

有人可以给我一个提示如何解决它吗?

【问题讨论】:

    标签: spring-boot-test


    【解决方案1】:

    为了解决问题,我们可以在执行主进程之前检查活动配置文件

        @Override
        public void run(String... args) throws Exception {
            if (!Stream.of(env.getActiveProfiles()).collect(Collectors.toCollection(ArrayList::new)).contains("test")) {
                myMainProcess();
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2020-03-24
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2023-04-06
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多