3

Is it possible to run an embedded instance of DynamoDB within a Spring application to be used by integration tests similar to what embedded-redis allows? https://github.com/kstyrc/embedded-redis

A sample blob by AWS seems to indicate this is possible: https://github.com/awslabs/aws-dynamodb-examples/blob/master/src/test/java/com/amazonaws/services/dynamodbv2/local/embedded/DynamoDBEmbeddedTest.java

However, the documentation is scant and when attempting to set up the Embedded DynamoDB in the way shown I get the following error:

AmazonDynamoDB ddb = DynamoDBEmbedded.create().amazonDynamoDB();

Throws:

Caused by: java.lang.NullPointerException
    at com.amazonaws.services.dynamodbv2.local.shared.access.sqlite.SQLiteDBAccess.initializeMetadataTables(SQLiteDBAccess.java:389)
    at com.amazonaws.services.dynamodbv2.local.shared.access.sqlite.SQLiteDBAccess.<init>(SQLiteDBAccess.java:225)
    at com.amazonaws.services.dynamodbv2.local.shared.access.sqlite.SQLiteDBAccess.<init>(SQLiteDBAccess.java:194)
    at com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded.create(DynamoDBEmbedded.java:45)
    at com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded.create(DynamoDBEmbedded.java:35)
    at com.wdpr.keyring.entitlements.EmbeddedDynamoDBConfig.embeddedDynamoDB(EmbeddedDynamoDBConfig.java:26)

Essentially for the Spring application I would be registering the AmazonDynamoDB instance created by DynamoDBEmbedded.create().amazonDynamoDB(); as my bean to be used in the TEST application context like so:

@TestConfiguration
public class EmbeddedDynamoDBConfig {

    @Bean
    AmazonDynamoDB embeddedDynamoDB() {
        return DynamoDBEmbedded.create().amazonDynamoDB();
    }
}

If this is not possible, what are some other potential solutions?

Adam Bronfin
  • 1,209
  • 3
  • 27
  • 43

1 Answers1

1

As you wrote, but you need to copy native libs and set library path before that. These answers worked for me: https://stackoverflow.com/a/39086207 and https://stackoverflow.com/a/39086207. There are both Gradle and Maven samples there. Maven sample includes Java code you need. As I wrote above, missing item is setting library.path as JUnit Rule did not work for me for Bean creation.

@TestConfiguration
public class EmbeddedDynamoDBConfig {
    public EmbeddedDynamoDBConfig() {
        // adjust this path as needed
        System.setProperty("sqlite4java.library.path", "build/libs-native");
    }

    @Bean
    @Primary
    public AmazonDynamoDB amazonDynamoDB() {
        return DynamoDBEmbedded.create().amazonDynamoDB();
    }
}

Note however that STS does not handle the Gradle task automatically. I had to run Gradle by hands and then only use STS's JUnit run. If you find a solution, please share. Did not test Maven.

Right now I seem to have some access issue for automated runs in console: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The security token included in the request is invalid. (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: UnrecognizedClientException; Request ID: ...). Not yet sure how to fix.

Vladimir
  • 11
  • 1
  • 1
    If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/19806623) – STaefi May 23 '18 at 11:11
  • 1
    No new question yet: I had to comment that my solution could be incomplete – Vladimir May 24 '18 at 07:52