Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test sns locally in an IDE like Intellij (for unit tests)

Tags:

amazon-sns

Is there a way we can locally instantiate SNS to be able to run unit tests ? I know dynamoDB can be locally instantiated using DynamoDBLocal, does SNS support the same ? If so, can anyone provide the documentation in being able to do so ? Thanks in advance.

like image 290
Naren Nala Avatar asked Mar 22 '15 17:03

Naren Nala


2 Answers

No, currently AWS doesn't provide a way of running SNS locally. There are however several ways we as developers can test our applications to ensure our good behaviour when integrating with external services.

Here are three options that you should consider:

  1. Integration Account
  2. Mocking
  3. FakeSNS

Amazon doesn't provide a sandbox area for testing applications against. However, there isn't anything stopping us from registering events against an integration layer.

As an example, register a topic at AWS called “NewAccountEvent-Dev” and provide this topic for integration testing. This will ensure the application is correctly constructing requests to AWS. Typically I'd expect most mature teams have several fully integrated test environments. Try and ensure that the regions before production are exactly the same as production.

As developers, we should try and make sure that we pick up issues as early as possible. Picking up integration issues after the application has been deployed even in a test environment is quite far right in the development process. To improve upon our integration environment we can use a concept call mocks. Mocks will allow us to pick up issues at build time.

Mocking allows you to test if you're application will respond correctly to messages you expect from SNS. You'll manually setup the responses and begin writing your tests against fake SNS services. There are lots of mocking tools to help you quckly write mocks. The Language & Framework you've selected will end up driving the framework that you select. Personally, I'm most familiar with Mockito as I use it in spring-boot reguarly.

Finally, you can run your application and include a service which emulates SNS. This is similar to DynamoDBLocal FakeSNS. If you're writing an application that has heavy integration into SNS. i.e. deleting, creating, dealing with permissions etc. This project would be a good one look at. These serveres are richer as they generally implement logic within the service itself, rather then just mocking out the interface.

Unfortunately, Fake SNS doesn't seem to be much GitHub activity at the moment; so I'd encourage you to continue doing more research.

like image 133
Lance Avatar answered Nov 23 '22 23:11

Lance


Another good fake implemnetation of SNS is LocalStack

Localstack provides several fake implementations of amazon aws services. I am using it to test full sns / sqs communication locally and on CI. Works perfectly.

like image 45
Fino Avatar answered Nov 23 '22 22:11

Fino