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

Is there a way to localize SNS to run unit tests? I know that dynamoDB can be locally created using DynamoDBLocal, does SNS support the same thing? If so, can anyone provide documentation able to do this? Thanks in advance.

+11
source share
4 answers

No, AWS does not currently provide a way to start SNS locally. However, there are several ways developers can test our applications to ensure good behavior when integrating with external services.

Here are three options you should consider:

  1. Integration Account
  2. Tantalizing
  3. Fake sns

Amazon does not provide a sandbox zone for testing applications. However, there is nothing that will prevent us from registering events at the integration level.

As an example, register a theme in AWS called "NewAccountEvent-Dev" and provide this theme for integration testing. This will ensure that your AWS applications are built correctly. I usually expect most mature teams to have several fully integrated test environments. Try and make sure that the regions before production are exactly the same as the production.

As developers, we must make sure that we pick up the problems as early as possible. Solving integration problems after deploying the application, even in a test environment, is quite far in the development process. To improve our integration environment, we can use the concept of call mocks. Mocks will allow us to solve problems during assembly.

Mocking allows you to check whether the application will correctly respond to messages that you expect from SNS. You manually configure the answers and start writing your tests against fake SNS. There are many mocking tools to help you expose layouts. The selected language and angle will ultimately lead to the selection of the frame. Personally, I am most familiar with Mokito, since I use it in a spring boot.

Finally, you can start the application and enable a service that emulates SNS. This is similar to DynamoDBLocal FakeSNS . If you are writing an application that has great integration in SNS. those. delete, create, work with permissions, etc. This project will be a good look. These servers are richer because they usually implement logic within the service itself, rather than just mocking the interface.

Unfortunately, Fake SNS does not seem to have much GitHub activity at the moment; therefore, I would advise you to continue to do more research.

+5
source

Another Good False SNS Implementation - LocalStack

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

+2
source

Unfortunately, there is no SNS implementation that runs locally. Therefore, you must run the test layout using a utility such as mockito.

+1
source

We have a notificationService handler in which all SNS notifications are sent, calling it in code like this.

await ApplicationService.notification.notify(NotificationType.EMAIL_UPSERTED, { id: user.id.toString() }); 

This will call the notification notification function, which in the local environment will immediately notify, rather than send SNS. This works great to make sure the background code works.

  /** * @description notify addressee * @param {string} topic action or topic name * @param {object} message sns message object * @returns {Promise<void>} * @memberof NotificationService */ public async processNotification(topic: string, message: any):Promise<boolean> { Services.logger.debug('notification/notificationService.processNotification', { topic, message }); let status: boolean = false; switch (topic) { case NotificationType.EMAIL_UPSERTED: // do something break; default: break; } /** * @description send a notification via sns * @param {NotificationType} topic action or topic name * @param {object} message sns message object * @returns {Promise<void>} * @memberof NotificationService */ public async notify(topic: NotificationType, message: any): Promise<void> { Services.logger.debug('notification/notificationService.notify', { topic, message }); if (environment === EnvironmentType.LOCAL) { Services.logger.debug('processing ${topic} notification locally, by explicitly calling the processNotification'); await this.processNotification(topic, message); } else { await this.publishingService.publish(topic, message); } 

}

0
source

Source: https://habr.com/ru/post/984113/


All Articles