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

时间:2018-10-31 09:31:12

标签: java spring unit-testing spring-boot spring-cloud-aws

我已经按照spring documentation中的说明添加了spring-cloud-starter-aws-parameter-store-config依赖项。现在,对于单元测试,我想禁用参数存储配置。但是无法做到。

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

 aws.paramstore.enabled=false

也尝试从AwsParamStoreBootstrapConfiguration.class中排除AutoConfiguration,但仍然无法正常工作。

例外

  

原因:org.springframework.beans.BeanInstantiationException:   无法实例化   [com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement]:   工厂方法'ssmClient'抛出异常;嵌套异常为   com.amazonaws.SdkClientException:无法通过   区域提供商链。必须在构建器中提供一个明确的区域   或设置环境来提供区域。在   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)   〜[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]在   org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)   〜[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] ... 83个常见帧   省略的原因:com.amazonaws.SdkClientException:无法找到   通过区域供应商链来确定区域。必须提供一个明确的区域   在构建器或设置环境中提供区域。在   com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:371)   〜[aws-java-sdk-core-1.11.336.jar:na]在   com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:337)   〜[aws-java-sdk-core-1.11.336.jar:na]在   com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)   〜[aws-java-sdk-core-1.11.336.jar:na]在   com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder.defaultClient(AWSSimpleSystemsManagementClientBuilder.java:44)   〜[aws-java-sdk-ssm-1.11.336.jar:na]在   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 个答案:

答案 0 :(得分:2)

我能够找出解决方案。似乎SpringBootTest甚至在测试配置类之前和加载ssmClient之前都尝试加载application.properties。解决方案是通过在@SpringBootTest本身上指定属性来禁用参数存储

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

编辑:

通过将属性(aws.paramstore.enabled=false)添加到bootstrap.properties文件中,我可以使用其他方法禁用参数存储。这是一个简单得多的解决方案