首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring Cloud应用程序与AWS参数存储的集成测试

Spring Cloud应用程序与AWS参数存储的集成测试
EN

Stack Overflow用户
提问于 2020-03-29 22:08:20
回答 1查看 1.3K关注 0票数 3

如何执行Spring Boot应用程序从AWS Parameter Store (dependency org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config)读取属性的集成测试。

是否应在集成测试中禁用AWS参数存储集成?

如何在集成测试中使用本地服务器(或模拟)而不是真正的AWS参数存储?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-29 22:08:20

通常,出于简单性和性能考虑,应在集成测试中禁用与AWS参数存储的集成。相反,负载测试属性来自文件(例如,src/test/resources/test.properties)

代码语言:javascript
运行
复制
@SpringBootTest(properties = "aws.paramstore.enabled=false")
@TestPropertySource("classpath:/test.properties")
public class SampleTests {
  //...
}

如果个别测试需要检查与AWS Parameter Store的集成,请使用TestcontainersLocalStack,这是一个用于Docker的易于使用的本地AWS云堆栈。

添加一个配置类,创建AWSSimpleSystemsManagement类型的自定义AWS,将其配置为使用LocalStack,而不是使用真正的ssmClient Parameter Store在org.springframework.cloud.aws.autoconfigure.paramstore.AwsParamStoreBootstrapConfiguration中声明的默认bean。

代码语言:javascript
运行
复制
@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添加以下条目来将配置类添加到引导上下文中

代码语言:javascript
运行
复制
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.test.AwsParamStoreBootstrapConfiguration

同样的方法也可以用于使用Mockito模拟ssmClient

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60915364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档