# Testing serverless microservces

**URL:** <https://forum.serverless.com/t/testing-serverless-microservces/5273>\
**Category:** Serverless Architectures\
**Created:** [July 28, 2018, 5:11am UTC](https://forum.serverless.com/t/testing-serverless-microservces/5273 "2018-07-28T05:11:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![piersmacdonald](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/piersmacdonald/32/1292_2.png) [@piersmacdonald](https://forum.serverless.com/u/piersmacdonald)\
**Post date:** [July 28, 2018, 5:11am UTC](https://forum.serverless.com/t/testing-serverless-microservces/5273/1 "2018-07-28T05:11:37Z")

</div>

Instead of being general I’ll ask the question using my specific examples.

So I have a few serverless projects, each representing a microservice. There are two in particular, the OrderService and RulesEngine. When a new order is create it needs to send it to the rules engine to have any rules applied.

Now there are a few different ways to do this, two of which I considered:

1. OrderService says the order, publishes a NewOrder event via some message broker (queues, streams etc), RulesEngine picks its, applies the rules, publishes RuleApplied event, OrderService updates order.

2. OrderService calls ‘applyRules’ on RulesEngine, gets response and saves it.

I know (1) scales better but we went with (2) because this is a MVP and we didn’t want the hassle of configuring SQS etc whatever, dealing with failures etc. It was just easier to have something that fails so we can debug it.

Anyways, how would I test this? I can run the OrderService in offline mode but I don’t have a great way to simulate the call to the RulesEngine. Is there a way to provide a mock RulesEngine lambda at run time?

---

<div class="post-metadata">

**Author:** ![garethmcc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/garethmcc/32/2276_2.png) [@garethmcc](https://forum.serverless.com/u/garethmcc)\
**Post date:** [July 30, 2018, 9:59am UTC](https://forum.serverless.com/t/testing-serverless-microservces/5273/2 "2018-07-30T09:59:17Z")

</div>

For my own unit testing I like to use `serverless-mocha-plugin` as well as `aws-sdk-mock` so that I can mock any AWS services. And this where using something like SNS would have been a boon because you could mock any number of responses and hard code some test event if your two services communicated via SNS instead of synchronously via http.

However, there is a node module I have used that allows me to capture outgoing http requests and “respond” with whatever I like called `mitm` (man in the middle). You should, ideally, have tests that have no dependancy on any other service be they external third party or your own so these modules are very useful. At least I have found they are:

[https://www.npmjs.com/package/serverless-mocha-plugin](https://www.npmjs.com/package/serverless-mocha-plugin)  
[https://www.npmjs.com/package/aws-sdk-mock](https://www.npmjs.com/package/aws-sdk-mock)  
[https://www.npmjs.com/package/mitm](https://www.npmjs.com/package/mitm)

Hope that helps.

---

<div class="post-metadata">

**Author:** ![piersmacdonald](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/piersmacdonald/32/1292_2.png) [@piersmacdonald](https://forum.serverless.com/u/piersmacdonald)\
**Post date:** [July 30, 2018, 11:55pm UTC](https://forum.serverless.com/t/testing-serverless-microservces/5273/3 "2018-07-30T23:55:57Z")

</div>

That’s a great start, thanks!

I just noticed the SQS trigger support was added. We’re going to use those between services. Add in aws-sdk-mock and we should be off to the races

---

<div class="post-metadata">

**Author:** ![piersmacdonald](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/piersmacdonald/32/1292_2.png) [@piersmacdonald](https://forum.serverless.com/u/piersmacdonald)\
**Post date:** [July 31, 2018, 4:51pm UTC](https://forum.serverless.com/t/testing-serverless-microservces/5273/4 "2018-07-31T16:51:06Z")

</div>

So have you used `mitm` to do integration testing? II’m fiddling around with it right now. Is the general premise to write a mitm handler with a conditional looking for any calls writing to aws and then pipe those to the `aws-mock` library?

---

<div class="post-metadata">

**Author:** ![garethmcc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/garethmcc/32/2276_2.png) [@garethmcc](https://forum.serverless.com/u/garethmcc)\
**Post date:** [August 2, 2018, 7:04am UTC](https://forum.serverless.com/t/testing-serverless-microservces/5273/5 "2018-08-02T07:04:00Z")

</div>

@piersmacdonald mitm will capture all outgoing traffic you set it up to capture and then you can return whatever response you want. I only really use mitm when i cant use aws-sdk-mock; for example for other third party API’s. aws-sdk-mock does what mitm does essentially but is only for aws services and is a lot easier to setup which is why both are in my tool belt.
