2

I am trying to use DynamoDbLocal server for unit test cases. And came up with two options,

Either define a junit class rule which starts local server before class and stops it after class. So essentially it will start and stop server for each unit test class.

public class MyDynamoDbLocalServerRule extends ExternalResource {

@Override
protected void before() throws Throwable {
    myInMemoryDynamoDbServer.start();
}

@Override
protected void after() throws Throwable{
        inMemoryDynamoDbServer.stop();
}

OR

Singleton instance :

public static DynamoDBProxyServerContainer createInstance(final int  portToListenIn) {
    if (dynamoLocal == null) {
        synchronized (DynamoDBProxyServerContainer.class) {
            if (dynamoLocal == null) {
                dynamoLocal = new DynamoDBProxyServerContainer(portToListenIn);
            }
        }
    }
    return dynamoLocal;
}

private DynamoDBProxyServerContainer(final int portToListenIn) {
    this.startServer(portToListenIn);
    getRuntime().addShutdownHook(new Thread(() -> stopServer()));
}

Which one would you recommend and do you have better of doing this ? Please note i should be able to use it with Guice dependency injection framework.

sidss
  • 923
  • 1
  • 12
  • 20

1 Answers1

1

I would recommend Singleton approach as creating the database instance for each test case will be a time consuming option. Also, if you have many test cases, the unit testing is likely to take more time to complete. If you have continuous integration, the build and unit test would take more time.

As the unit tests run in sequential manner, you don't need separate instance for each test case.

notionquest
  • 37,595
  • 6
  • 111
  • 105