繁体   English   中英

Spring Cloud 应用程序与 AWS Parameter Store 的集成测试

[英]Integration testing of a Spring Cloud application with the 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 的集成,请使用TestcontainersLocalStack一个易于使用的本地 AWS Docker 云堆栈。

添加配置类创建自定义ssmClient型豆AWSSimpleSystemsManagement配置为使用LocalStack而不是宣布了一个默认的org.springframework.cloud.aws.autoconfigure.paramstore.AwsParamStoreBootstrapConfiguration使用AWS实际参数商店。

@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

同样的方法也可以用于嘲讽ssmClient使用的Mockito。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM