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

14

根据spring文档的说明,我添加了spring-cloud-starter-aws-parameter-store-config依赖项。现在,对于单元测试,我想禁用参数存储配置,但无法实现。

我尝试在test/application.properties中设置以下属性:

 aws.paramstore.enabled=false

尝试排除 AwsParamStoreBootstrapConfiguration.classAutoConfiguration中,但仍未能正常工作。

异常

Caused by: org.springframework.beans.BeanInstantiationException: 无法实例化 [com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement]: 工厂方法 'ssmClient' 抛出异常;嵌套异常为 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]

1个回答

23

我能使用另一种方法禁用paramstore,只需将(aws.paramstore.enabled=false) 属性添加到 bootstrap.properties 文件的测试资源文件夹中即可。这是一种更简单的解决方案。

旧的解决方案

我找出了解决方案。看起来 SpringBootTest 甚至在测试配置类之前加载 ssmClient ,以及在加载 application.properties 之前。解决方案是通过在 @SpringBootTest 上指定属性来禁用 paramstore。

@SpringBootTest(classes = MyApp.class, properties = {"aws.paramstore.enabled=false"})

@sidgate:如果我们在引导程序中添加属性并将其禁用,则需要为所有其他需要该属性的环境启用它。 - Som
只需在 src/test/resources/bootstrap.properties 中添加即可。 - sidgate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接