使用配置文件时,SpringBoot测试无法自动装配服务

问题描述

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

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

如果我在Test课程中没有Autowried,这是可行的

如果我将Autowired标注与ActiveProfiles标注一起使用,它将无法加载服务组件类。

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

解决方法

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

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

    }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...