如何执行Spring Boot应用程序从AWS Parameter Store (dependency org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config
)读取属性的集成测试。
是否应在集成测试中禁用AWS参数存储集成?
如何在集成测试中使用本地服务器(或模拟)而不是真正的AWS参数存储?
发布于 2020-03-29 22:08:20
通常,出于简单性和性能考虑,应在集成测试中禁用与AWS参数存储的集成。相反,负载测试属性来自文件(例如,src/test/resources/test.properties
)
@SpringBootTest(properties = "aws.paramstore.enabled=false")
@TestPropertySource("classpath:/test.properties")
public class SampleTests {
//...
}
如果个别测试需要检查与AWS Parameter Store的集成,请使用Testcontainers和LocalStack,这是一个用于Docker的易于使用的本地AWS云堆栈。
添加一个配置类,创建AWSSimpleSystemsManagement
类型的自定义AWS,将其配置为使用LocalStack,而不是使用真正的ssmClient
Parameter Store在org.springframework.cloud.aws.autoconfigure.paramstore.AwsParamStoreBootstrapConfiguration
中声明的默认bean。
@Configuration(proxyBeanMethods = false)
public class AwsParamStoreBootstrapConfiguration {
public static final LocalStackContainer AWS_SSM_CONTAINER = initContainer();
public static LocalStackContainer initContainer() {
LocalStackContainer container = new LocalStackContainer().withServices(SSM);
container.start();
Runtime.getRuntime().addShutdownHook(new Thread(container::stop));
return container;
}
@Bean
public AWSSimpleSystemsManagement ssmClient() {
return AWSSimpleSystemsManagementClientBuilder.standard()
.withEndpointConfiguration(AWS_SSM_CONTAINER.getEndpointConfiguration(SSM))
.withCredentials(AWS_SSM_CONTAINER.getDefaultCredentialsProvider())
.build();
}
}
只要AwsParamStorePropertySourceLocator
是由Spring Cloud“引导”上下文加载的,您就需要通过向文件src/test/resources/META-INF/spring.factories
添加以下条目来将配置类添加到引导上下文中
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.test.AwsParamStoreBootstrapConfiguration
同样的方法也可以用于使用Mockito模拟ssmClient
。
https://stackoverflow.com/questions/60915364
复制相似问题