DynamoDB fake server for integration testing

João Matos :

I am new to DynamoDB and I was wondering if there is any kind of embedded DynamoDB fake server available as a maven dependency/plugin, in order to run end-to-end tests as part of the maven build. E.g. something like Achilles but for DynamoDB instead of Cassandra.

Worth mentioning that I found this project, but I found it to be too unstable and I was not able to get it to work.

Thank you for your help.

João Matos :

I found two possible approaches, DynamoDBEmbedded and Localstack. Regarding the latter, as disclaimed in the website:

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.

It mocks several aws services including dynamodb. Example:

1) In my pom.xml under dependencies:

<dependency>
   <groupId>cloud.localstack</groupId>
   <artifactId>localstack-utils</artifactId>
   <version>0.1.19</version>
   <scope>test</scope>
</dependency>

2) Add these headers to you unit test class:

@RunWith(LocalstackDockerTestRunner.class)
@LocalstackDockerProperties(randomizePorts = true, services = {"dynamodb"})

3) Make sure your application is pointing to localstack url, e.g. dynamo will wait for you at http://localhost:4569. More information here.


Regarding the former, i.e. DynamoDBEmbedded, we can do the following:

1) In pom.xml under dependencies:

<dependency>
   <groupId>com.amazonaws</groupId>
   <artifactId>DynamoDBLocal</artifactId>
   <version>1.11.477</version>
   <scope>test</scope>
</dependency>

2) Then in our unit test:

private AmazonDynamoDB amazonDynamoDB;

@Before
public void setup() throws Exception {

    amazonDynamoDB =  DynamoDBEmbedded.create().amazonDynamoDB();//dynamoDB.getAmazonDynamoDB();
    amazonDynamoDB.createTable(new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(ITEM).withKeyType(KeyType.HASH))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(ITEM).withAttributeType(ScalarAttributeType.S))
            .withProvisionedThroughput(new ProvisionedThroughput(10L, 15L))
    );
}

And make sure that our DAOs use this amazonDynamoDB instance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=143117&siteId=1
Recommended