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
).
Should the AWS Parameter Store integration be disabled in integration tests?
How to use local server (or mock) instead of the real AWS Parameter Store in integration tests?
Usually integration with the AWS Parameter Store should be disabled in integration tests for simplicity and performance. Instead, load test properties from a file (e.g., 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.
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.
@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
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.test.AwsParamStoreBootstrapConfiguration
The same approach can be used for mocking ssmClient
using Mockito.