Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring app run DynamoDBEmbedded for integration testing?

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?

like image 875
Adam Bronfin Avatar asked Oct 24 '25 02:10

Adam Bronfin


1 Answers

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.

like image 61
Vladimir Avatar answered Oct 26 '25 16:10

Vladimir