简体   繁体   English

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

[英]Integration testing of a Spring Cloud application with the AWS Parameter Store

How to perform integration testing of a Spring Boot application reading properties from the AWS Parameter Store (dependency org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config ).如何对从 AWS Parameter Store 读取属性的 Spring Boot 应用程序执行集成测试(依赖关系org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config )。

Should the AWS Parameter Store integration be disabled in integration tests?是否应该在集成测试中禁用 AWS Parameter Store 集成?

How to use local server (or mock) instead of the real AWS Parameter Store in integration tests?如何在集成测试中使用本地服务器(或模拟)而不是真正的 AWS Parameter Store?

Usually integration with the AWS Parameter Store should be disabled in integration tests for simplicity and performance.为了简单和性能,通常应该在集成测试中禁用与 AWS Parameter Store 的集成。 Instead, load test properties from a file (eg, src/test/resources/test.properties )相反,从文件加载测试属性(例如, src/test/resources/test.properties

@SpringBootTest(properties = "aws.paramstore.enabled=false")
@TestPropertySource("classpath:/test.properties")
public class SampleTests {
  //...
}

If individual tests need to check integration with the AWS Parameter Store use Testcontainers and LocalStack an easy-to-use local AWS cloud stack for Docker.如果单个测试需要检查与 AWS Parameter Store 的集成,请使用TestcontainersLocalStack一个易于使用的本地 AWS Docker 云堆栈。

Add a configuration class creating custom ssmClient bean of type AWSSimpleSystemsManagement configured to use LocalStack instead of a default one declared in org.springframework.cloud.aws.autoconfigure.paramstore.AwsParamStoreBootstrapConfiguration using the real AWS Parameter Store.添加配置类创建自定义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();
  }
}

As far as AwsParamStorePropertySourceLocator is a loaded by a Spring Cloud "bootstrap" context, you need to add a configuration class to the bootstrap context by adding to the file src/test/resources/META-INF/spring.factories the following entry至于AwsParamStorePropertySourceLocator是由 Spring Cloud“引导程序”上下文加载的,您需要通过将以下条目添加到文件src/test/resources/META-INF/spring.factories来将配置类添加到引导程序上下文

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.test.AwsParamStoreBootstrapConfiguration

The same approach can be used for mocking ssmClient using Mockito.同样的方法也可以用于嘲讽ssmClient使用的Mockito。

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

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