Java spring启动-集成测试自动连线接口未找到此类bean

Java spring启动-集成测试自动连线接口未找到此类bean,java,spring-boot,junit5,spring-boot-test,Java,Spring Boot,Junit5,Spring Boot Test,我有一个spring boot应用程序,现在需要支持多个对象存储,并根据环境选择使用所需的存储。基本上,我所做的是创建一个接口,然后由每个存储库实现 我简化了示例的代码。 我根据决定环境的spring配置文件为每种商店类型创建了2个bean: @Profile("env1") @Bean public store1Sdk buildClientStore1() { return new store1sdk(); } @Profile("

我有一个spring boot应用程序,现在需要支持多个对象存储,并根据环境选择使用所需的存储。基本上,我所做的是创建一个接口,然后由每个存储库实现

我简化了示例的代码。 我根据决定环境的spring配置文件为每种商店类型创建了2个bean:

  @Profile("env1")
  @Bean
  public store1Sdk buildClientStore1() {
     return new store1sdk();
  }

  @Profile("env2")
  @Bean
  public store2Sdk buildClientStore2() {
     return new store2sdk();
  }

在服务层,我自动连接了接口,然后在存储库中,我使用@Profile指定要使用的接口实例

public interface ObjectStore {
  String download(String fileObjectKey);
  ...
}

@Service
public class ObjectHandlerService {

  @Autowired
  private ObjectStore objectStore;

  public String getObject(String fileObjectKey) {
    return objectStore.download(fileObjectKey);
  }
  ...
}

@Repository
@Profile("env1")
public class Store1Repository implements ObjectStore {
  @Autowired
  private Store1Sdk store1client;

  public String download(String fileObjectKey) {
    return store1client.getObject(storeName, fileObjectKey);
  }
}
当我使用配置的“env”启动应用程序时,它实际上会按预期运行。但是,在运行测试时,我得到“没有ObjectStore类型的合格bean。至少需要1个符合autowire候选条件的bean。”

正如测试类上的@ActiveProfile中所指出的,还有一些其他环境,例如dev、test、prod。我尝试过使用组件扫描,将impl和interface放在同一个包中,但没有成功。我觉得我在测试设置中遗漏了一些明显的东西。但可能是我的整个应用程序配置有问题吗?我的解决方案的主要目的是避免有太多的问题

    if (store1Sdk != null) {
      store1Sdk.download(fileObjectKey);
    }
    if (store2Sdk != null) {
      store2Sdk.download(fileObjectKey);
    }

此问题是因为Store1Repository使用
@Profile(“env1”)
,当您使用
@test
时,不会调用此类。尝试删除
Store1Repository
@Profile(“env1”)

如果您使用
@test
,则两个
store1Sdk/store2Sdk
都不实例化,请尝试添加默认的
实例化。例如

@Bean    
public store2Sdk buildClientStoreDefault() {
    return new store2sdk();
}
尝试
@ActiveProfiles({“env1”,“test”})


使用
@ActiveProfiles
激活多个配置文件,并将配置文件指定为一个数组。

如果从存储库实现中删除配置文件注释,是否会出现相反的问题,即尝试实例化ObjectStore的2个实例?如果使用@test,则store1Sdk/store2Sdk都不会实例化,尝试添加默认实例。例如:成功了。谢谢
@Bean    
public store2Sdk buildClientStoreDefault() {
    return new store2sdk();
}