Evg*_*yst 4 spring amazon-web-services spring-boot spring-cloud aws-parameter-store
如何对从 AWS Parameter Store 读取属性的 Spring Boot 应用程序执行集成测试(依赖项org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config)。
是否应该在集成测试中禁用 AWS Parameter Store 集成?
如何在集成测试中使用本地服务器(或模拟)而不是真正的AWS Parameter Store?
通常,为了简单性和性能,应在集成测试中禁用与 AWS Parameter Store 的集成。相反,从文件加载测试属性(例如,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 云堆栈)。
添加一个配置类,创建配置为使用 LocalStackssmClient类型的自定义 bean AWSSimpleSystemsManagement,而不是org.springframework.cloud.aws.autoconfigure.paramstore.AwsParamStoreBootstrapConfiguration使用实际 AWS Parameter Store 中声明的默认 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。
| 归档时间: | 
 | 
| 查看次数: | 2369 次 | 
| 最近记录: |