LocalStack Testing Tools

Tools to simplify application testing

Covered Topics

JVM Testing Utils

LocalStack provides Java Utils library that integrates with JUnit and provides LocalStack-targeted AWS Clients.

Installation

  1. <dependency>
  2. <groupId>cloud.localstack</groupId>
  3. <artifactId>localstack-utils</artifactId>
  4. <version>0.2.15</version>
  5. <scope>test</scope>
  6. </dependency>
  1. testImplementation group: 'cloud.localstack', name: 'localstack-utils', version: '0.2.15'

Usage

  1. ...
  2. import cloud.localstack.LocalstackTestRunner;
  3. import cloud.localstack.ServiceName;
  4. import cloud.localstack.TestUtils;
  5. import cloud.localstack.docker.annotation.LocalstackDockerProperties;
  6. @RunWith(LocalstackTestRunner.class)
  7. @LocalstackDockerProperties(services = { ServiceName.S3, "sqs", "kinesis" })
  8. public class MyCloudAppTest {
  9. @Test
  10. public void testLocalS3API() {
  11. AmazonS3 s3 = TestUtils.getClientS3()
  12. List<Bucket> buckets = s3.listBuckets();
  13. ...
  14. }
  15. }
  1. ...
  2. import cloud.localstack.LocalstackTestRunner
  3. import cloud.localstack.ServiceName
  4. import cloud.localstack.TestUtils
  5. import cloud.localstack.docker.annotation.LocalstackDockerProperties
  6. @RunWith(LocalstackTestRunner::class)
  7. @LocalstackDockerProperties(services = [ServiceName.S3, "sqs", "kinesis"])
  8. public class MyCloudAppTest {
  9. @Test
  10. fun testLocalS3API() {
  11. val s3 = TestUtils.getClientS3()
  12. val buckets = s3.listBuckets();
  13. ...
  14. }
  15. }

Powermock

You can use the PowerMock Library to call the builders default method and still get LocalStack version of the AWS clients.

  1. ...
  2. @RunWith(PowerMockRunner.class)
  3. @PowerMockRunnerDelegate(LocalstackTestRunner.class)
  4. @LocalstackDockerProperties(services = { "ses" })
  5. @PrepareForTest({ AmazonSimpleEmailServiceClientBuilder.class, AmazonSimpleEmailServiceAsyncClientBuilder.class })
  6. @PowerMockIgnore({"javax.crypto.*", "org.hamcrest.*", "javax.net.ssl.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*", "javax.security.*", "org.w3c.*"})
  7. public class SESMessagingTest {
  8. ....
  9. @Before
  10. public void mockSES() {
  11. AmazonSimpleEmailService mockSes = TestUtils.getClientSES();
  12. PowerMockito.mockStatic(AmazonSimpleEmailServiceClientBuilder.class);
  13. when(AmazonSimpleEmailServiceClientBuilder.defaultClient()).thenReturn(mockSes);
  14. }
  15. @Test
  16. public void testSendEmail() throws Exception {
  17. AmazonSimpleEmailService client = amazonSimpleEmailServiceClientBuilder.defaultClient();
  18. ....

PowerMockLocalStack Utility

This utility makes easier to use PowerMock with LocalStack.

  1. ...
  2. public class PowerMockLocalStackExampleTest extends PowerMockLocalStack{
  3. private static final String TOPIC = "topic";
  4. @Before
  5. public void mock() {
  6. PowerMockLocalStack.mockSNS();
  7. }
  8. @Test
  9. public void testSendMessage() throws JMSException {
  10. final AmazonSNS clientSNS = AmazonSNSClientBuilder.defaultClient();
  11. ...
  12. }
  13. }

Last modified May 17, 2022: fix capitalization of LocalStack in affected files (#157) (6206611c)