簡體   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