java如何为测试禁用AWS参数存储自动配置?
我添加了spring-cloud-starter-aws-parameter-store-config
依赖项,如spring documentation中所述。现在,对于单元测试,我想禁用参数存储配置。但我做不到
我尝试在测试/应用程序中设置以下属性。属性
aws.paramstore.enabled=false
还尝试将AwsParamStoreBootstrapConfiguration.class
从AutoConfiguration
中排除,但仍不起作用
例外情况
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]
# 1 楼答案
通过将属性(
aws.paramstore.enabled=false
)添加到bootstrap.properties
文件测试资源文件夹中,我可以用另一种方法禁用paramstore。这是一个简单得多的解决方案旧的解决方案
我找到了解决办法。似乎
SpringBootTest
甚至在测试配置类之前和加载application.properties
之前都试图加载ssmClient
。解决方案是通过指定@SpringBootTest
本身的属性来禁用paramstore