如何为测试禁用 AWS 参数存储自动配置?

How to disable AWS parameter store autoconfiguration for tests?

我添加了 spring-cloud-starter-aws-parameter-store-config 依赖项,如 spring documentation 中所述。现在,对于单元测试,我想禁用参数存储配置。但是做不到。

我尝试在 test/application.properties

中按照 属性 进行设置
 aws.paramstore.enabled=false

也尝试从 AutoConfiguration 中排除 AwsParamStoreBootstrapConfiguration.class 但仍然无效。

异常

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement]: Factory method 'ssmClient' threw exception; nested exception is com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] ... 83 common frames omitted Caused by: com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region. at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:371) ~[aws-java-sdk-core-1.11.336.jar:na] at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:337) ~[aws-java-sdk-core-1.11.336.jar:na] at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46) ~[aws-java-sdk-core-1.11.336.jar:na] at com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder.defaultClient(AWSSimpleSystemsManagementClientBuilder.java:44) ~[aws-java-sdk-ssm-1.11.336.jar:na] at org.springframework.cloud.aws.autoconfigure.paramstore.AwsParamStoreBootstrapConfiguration.ssmClient(AwsParamStoreBootstrapConfiguration.java:53) ~[spring-cloud-starter-aws-parameter-store-config-2.0.0.RELEASE.jar:2.0.0.RELEASE]

我可以通过将 属性 (aws.paramstore.enabled=false) 添加到 bootstrap.properties 文件测试资源文件夹来使用替代方法禁用 paramstore。这是一个更简单的解决方案

旧的解决方案

我找到了解决方案。似乎 SpringBootTest 甚至在测试配置 class 和加载 application.properties 之前尝试加载 ssmClient。解决方案是通过在 @SpringBootTest 本身

上指定 属性 来禁用 paramstore
@SpringBootTest(classes = MyApp.class, properties = {"aws.paramstore.enabled=false"})