Mocking Services made easy with Mountebank

Vinaykumar Gupta
5 min readMay 24, 2020

Have you ever been in a situation where you are about to start your integration test and one of the services is not yet ready? The only thing we can do is test all the scenarios related to that service in a later cycle. But wait. What if we can mock those service by just writing EJS file (Embedded Javascript Templates) without using any real dependency and running those mocked service independently. Yes we can do that using MounteBank. In the modern time where software development is moving towards service-oriented architectures (SOA), programs often need to call multiple services to run applications.

What does the Mocking of service mean?

Mocking of service means a service mock code is written that simulates the service that you would use in the product but is lighter, less complex, and easier to control than the actual service you would use in production.

You can set a mock service to return a default response or specific test data, then run the application you’re interested in testing as if the dependent service were really there. Because of this, having a flexible way to mock services can make your workflow faster and more efficient. Sometimes mocking of service is also called as Service Virtualization.

About MounteBank

Mountebank is a free and open source service-mocking tool that you can use to mock HTTP services, including REST and SOAP services. You can also use it to mock SMTP or TCP requests.

Prerequisites

  • NodeJS should be installed if you are planning to use Mountebank as an npm package.
  • Docker should be installed if you are planning to use Mountebank docker image.

Setup Mountebank to use it as Docker container

  • Pull docker image from docker hub using below command:
docker pull bbyars/mountebank
  • As we will be using docker compose to spawn mountebank container. Use below snippet and save it as docker-compose.yml file.
version: "3"services:mountebank:container_name: mountebankimage: bbyars/mountebank:latestvolumes:- ./imposters:/impostersports:- 2525:2525- 8090:8090command: mb --configfile /imposters/imposters.ejs --allowInjection

Note: In the above snippet “2525” is port where MounteBank will be hosted and “8090” is the port where your mocked service will be hosted.

Setup Mountebank using npm package install

  • Use below command to install Mountebank as npm package.
npm install -g mountebank

Creating First Mocked Service

As mountebank communicates using REST API. You can manage the resources of your Mountebank instance by sending HTTP requests to the different endpoints. To add a mock service, you send an HTTP POST request to the imposters endpoint. An imposter is a name for a mock service in Mountebank. Imposters can be simple or complex, depending on the behaviours you want in your mock.

Each imposters contains stubs. Stubs are configuration sets that determine the response that an imposter will give. Stubs can be further divided into combinations of predicates and responses. A predicate is a rule that triggers the imposter’s response. Predicates can use lots of different types of information, including URLs, request content (using XML or JSON), and HTTP methods.

Let’s create our first simple Imposters by writing this simple code:

  • User below code and save it as imposters.ejs file.
{ "imposters":[{"protocol": "http","port": 8090,"stubs": [{"responses": [{"is": {"statusCode": 200,"headers": { "Content-Type": "application/json" },"body": {"message":"Hello World!!"}}}]}]}]}

Note: In the above code we are creating one imposter that will listen on port 8090 only through the http protocol. In this example we are creating only one stub. As its an array, similarly we can create multiple stubs as per your required scenarios.

Posting Imposters to Mountebank

  • Using Docker Compose: As we have already mapped our host volume with the container. In docker-compose.yml file we have already written our command under “command” section where we are passing the imposters.ejs file as configfile to be picked up for posting. User below command.
docker-compose up -d
  • Using npm command for NodeJS setup:
mb --configfile /imposters/imposters.ejs --allowInjection

Now let’s check if our imposter is actually running and give the same response by sending a GET request to http://localhost:8090. You will get response like below.

Mocked Service Response

Deleting Imposter from MounteBank

We will send a DELETE request to http://localhost:2525/imposters/8090 to delete created imposter on port 8090. Here we need to replace the last post number with the once on which imposter is hosted i.e 8090 in my case.

Similarly, we can create some complex imposter as well and Run the same we did for the first one

Let’s create some more complex Imposters by writing below code:

{"imposters":[{"protocol": "http","port": 8090,"stubs": [{"predicates": [{"equals": {"method": "GET","path": "/test","query": {"greet":"morning"}}}],"responses": [{"is": {"statusCode": 200,"headers": { "Content-Type": "application/json" },"body": {"message":"Good Morning"}}}]},{"predicates": [{"equals": {"method": "GET","path": "/test","query": {"greet":"evening"}}}],"responses": [{"is": {"statusCode": 200,"headers": { "Content-Type": "application/json" },"body": {"message":"Good Evening"}}}]}]}]}

Note: In the above code we are creating conditional stub where the response is sent based on the inputs received. Let’s say if user sends GET request query parameter “Greet=Morning” the response will be sent as “Good Morning” and for “Greet=Evening” the response will be “Good Evening”.

GET Request: http://localhost:8090/test?greet=morning

GET Request: http://localhost:8090/test?greet=evening

You will receive response like below:

Mocked Service Response

Start Using Mountebank

If you enjoyed this overview about mountebank and want to explore more about it. Hit this Mountebank link which will walk you through multiple examples that are present on the mountebank website or you can access your local mountebank instance as well using link http://localhost:2525 once you start mountebank container.

If you always wanted to learn something new and can commit the time, now it is a great time as ever with new trends in Software Development Architecture you can surely achieve those.

If you like this blog and thought it can be useful do share with your friends and colleagues to help them solve these challenges as well and let them explore more.

So what you are waiting for? Share your thoughts and feedback with me on Linked or Twitter.

Happy Learning!!!

--

--