Featured image of post Mountebank ina Nutshell

Mountebank ina Nutshell

Mock APIs, databases, and services

Mountebank: The Swiss Army Knife of API Mocking

What is Mountebank?

βœ… Mocking REST, SOAP, TCP, and SMTP services
βœ… Testing APIs before they even exist
βœ… Simulating slow or flaky services
βœ… Injecting errors to test edge cases
βœ… Running everything in a lightweight Node.js-based setup


How Does Mountebank Work?

It works by creating impostersβ€”fake services that act like the real thing.

Here’s how it goes down:

  1. Install & Run Mountebank
  2. Create an imposter (fake service)
  3. Send requests to the imposter instead of the real API
  4. Mountebank responds just like a real service

Sounds cool? Let’s set it up! πŸš€


Setting Up Mountebank

πŸ› οΈ 1. Install Mountebank

Since Mountebank runs on Node.js, installing it is as easy as:

1
npm install -g mountebank

Then, start Mountebank with:

1
mb --allowInjection

By default, it runs on port 2525 and is ready to create imposters.


πŸ”§ 2. Create a Mock API (Imposter)

Now, let’s create a fake API that returns user data when you hit /user/123.

Option 1: Using a JSON Config File

Create a file called imposter.json:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
{
  "port": 3000,
  "protocol": "http",
  "stubs": [
    {
      "predicates": [
        { "equals": { "method": "GET", "path": "/user/123" } }
      ],
      "responses": [
        {
          "is": {
            "statusCode": 200,
            "headers": { "Content-Type": "application/json" },
            "body": "{ \"id\": 123, \"name\": \"John Doe\" }"
          }
        }
      ]
    }
  ]
}

Now, load this imposter into Mountebank:

1
curl -X POST http://localhost:2525/imposters -H "Content-Type: application/json" -d @imposter.json

Boom! πŸŽ‰ Now, calling http://localhost:3000/user/123 will return:

1
2
3
4
{
  "id": 123,
  "name": "John Doe"
}

Option 2: Using the HTTP API

You can also create an imposter dynamically using Mountebank’s API:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
curl -X POST http://localhost:2525/imposters -H "Content-Type: application/json" -d '{
  "port": 3000,
  "protocol": "http",
  "stubs": [
    {
      "predicates": [
        { "equals": { "method": "GET", "path": "/user/123" } }
      ],
      "responses": [
        {
          "is": {
            "statusCode": 200,
            "headers": { "Content-Type": "application/json" },
            "body": "{ \"id\": 123, \"name\": \"John Doe\" }"
          }
        }
      ]
    }
  ]
}'

Now, localhost:3000/user/123 is a fully functional mock API!


Simulating Failures and Delays

Want to test how your app handles timeouts or errors?

πŸ•’ Simulating a Slow Response

1
2
3
4
5
6
7
8
9
{
  "is": {
    "statusCode": 200,
    "body": "{ \"message\": \"This took forever...\" }",
    "_behaviors": {
      "wait": 5000
    }
  }
}

Now, calling this endpoint will delay for 5 seconds before responding.

πŸ”₯ Simulating a 500 Server Error

1
2
3
4
5
6
{
  "is": {
    "statusCode": 500,
    "body": "{ \"error\": \"Internal Server Error\" }"
  }
}

Perfect for testing how your app handles failures!


Mountebank vs. WireMock

So, which one should you use?

FeatureMountebankWireMock
Supports HTTP APIs?βœ… Yesβœ… Yes
Supports SOAP?βœ… Yes❌ No
Supports TCP & SMTP?βœ… Yes❌ No
Runs on Node.js?βœ… Yes❌ No (Java-based)
Dynamic API creation?βœ… Yesβœ… Yes
Best for?Full service virtualizationMocking REST APIs

If you’re only working with HTTP-based APIs, WireMock is great.

But if you need to mock multiple protocols (HTTP, TCP, SMTP, etc.), Mountebank is the way to go.


Why Mountebank is Awesome

βœ… Mock Anything – Not just HTTP, but also TCP, SMTP, and more.
βœ… Super Fast – No need to spin up full services; imposters respond instantly.
βœ… Great for CI/CD – Easily create and destroy mock services on demand.
βœ… Open Source & Lightweight – Runs on Node.js with zero dependencies.
βœ… Powerful Failure Simulation – Test real-world failures without breaking things.


πŸ”‘ Key Ideas

Key IdeaSummary
What is Mountebank?A service virtualization tool for mocking APIs, TCP, and SMTP services.
Why use it?Develop and test without relying on real services.
How to set it up?Install via npm, create imposters, and run locally.
Can it simulate failures?Yes, you can inject delays, errors, and timeouts.
Best for?Mocking multiple protocols in complex testing scenarios.