In my last post I wrote about the general approaches to testing if you’re looking at organisational level structuring. This time around I’ll be looking at the benefits for an individual developer.

I’d really hope that if you’re here, you already appreciate the benefits of unit tests, otherwise you might want to consider some of the following before diving in too far. Its not a pre-requisite, but might give you an appreciation of where I’m coming from:

I’m also assuming you’re already familiar with JUnit testing approaches, so I won’t be trying to spoon-feed you every line of syntax, instead focusing on the interesting aspects.

Starting with LocalStack

LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications. It spins up a testing environment on your local machine that provides the same functionality and APIs as the real AWS cloud environment. LocalStack.cloud

That’s a pretty nice elevator pitch of LocalStack’s aims, ambitions and capabilities. I’d normally try not just parrot a product’s documentation back here, instead I’d encourage you to go research it yourself… but their documentation is not at this stage fantastic.

So how do you get quickly started with Java?

Prerequisites:

  • JDK 11 or higher
    • JDK11 might not be strictly necessary, but I tend to use the highest LTS versions I can in my demos
  • Maven or similar dependancy tool
  • Docker

Assuming you’re using Maven, add the following dependancy into your pom.xml file:

<!-- AWS DynamoDB SDK  -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.733</version>
</dependency>

<!-- LocalStack Java Util -->
<dependency>
<groupId>cloud.localstack</groupId>
<artifactId>localstack-utils</artifactId>
<version>0.2.1</version>
<scope>test</scope>
</dependency>

This is adding in the import for Amazon DynamoDB’s java sdk, and a set of java utilities for working with LocalStack.

And once you’ve got that dependancy setup, add the following annotations to the top of your test case:

import cloud.localstack.LocalstackTestRunner;
import cloud.localstack.TestUtils;
import cloud.localstack.docker.annotation.LocalstackDockerProperties;

@RunWith(LocalstackTestRunner.class)
@ExtendWith({
LocalstackDockerExtension.class
})
@LocalstackDockerProperties(
services = { "dynamodb" },
useSingleDockerContainer = true
)
public class MyAppTest {
...
}

That’s actually all you need to get a DynamoDB container up and running… its pretty simple. LocalStack’s JavaUtil project even gives a way to grab access to the container connection:

Obtaining a connection to the Dynamo Container

AmazonDynamoDB dynamoDb = TestUtils.getClientDynamoDB();

I’ll be expanding on this in the next few posts, but starting with a basic understanding of the LocalStack util will form the basis of what we’re going to build.


Check out the other posts in this series I’ll come back and provide the links to the follow up posts here once they’ve been updated:

  1. Development with AWS & LocalStack
  2. Unit Testing Java and DyanmoDB, with JUnit & LocalStack (This Post)
  3. Configuration through Annotation
  4. Building a @Dataset

Find me on ...