0

I have an application module that installs a DynamoDB module

install(new DynamoDBModule());

In DynamoDBModule we have some code to build the DynamoDb client and initialize the mapper

 AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://prod-endpoint:8000", "us-west-2"))
        .build();

Now, when I am writing tests, I have to replace this dynamoDB endpoint with a local endpoint and I was wondering what is the simplest way to do this. I am aware of this stackoverflow question, but it would mean writing a lot of code for just a single small change. I would have to create a mock dynamodb module, a mock application module and then I can run something like this in my tests

Guice.createInjector(Modules.override(new AppModule()).with(new TestAppModule()));

Is there a simple way to somehow use or override the test endpoint when running tests and continue using the prod endpoint otherwise.

1 Answers1

0

Configure an EndpointConfiguration as binding and override it in TestAppModule. E.g.:

class DynamoDBModule {
   @Provides
   @Singleton
   AmazonDynamoDB provideAmazonDynamoDB(EndpointConfiguration endpointConfiguration) {
       return AmazonDynamoDBClientBuilder.standard()
          .withEndpointConfiguration(endpointConfiguration)
          .build()
   }

   @Provides
   @Singleton
   EndpointConfiguration provideEndpointConfiguration() {
       return new AwsClientBuilder.EndpointConfiguration("http://prod-endpoint:8000", "us-west-2");
   }
}

class TestAppModule {

   @Provides
   @Singleton
   EndpointConfiguration provideTestEndpointConfiguration() {
       return new AwsClientBuilder.EndpointConfiguration("test-value", "us-west-2");
   }
}

Then use you approach with Modules.override and it should work:

Guice.createInjector(Modules.override(new AppModule()).with(new TestAppModule()));
Yurii Melnychuk
  • 858
  • 1
  • 5
  • 11
  • 1
    I think this would work. I have another follow up question: what if I have another module let's say, sqs module. Now, I want to override endpoint for both sqs and DynamoDB. How would I do that? Using @Named ? If you could edit your answer and add that it would really help –  Feb 05 '22 at 11:36
  • 1
    I think you have answered my question already @Yurii and that the above should be a different question altogether. I will ask [another question](https://stackoverflow.com/q/70999405/18118996) and accept this as the answer to this. –  Feb 05 '22 at 15:34