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:
- Install & Run Mountebank
- Create an imposter (fake service)
- Send requests to the imposter instead of the real API
- 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:
|
|
Then, start Mountebank with:
|
|
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
:
|
|
Now, load this imposter into Mountebank:
|
|
Boom! π Now, calling http://localhost:3000/user/123 will return:
|
|
Option 2: Using the HTTP API
You can also create an imposter dynamically using Mountebankβs API:
|
|
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
|
|
Now, calling this endpoint will delay for 5 seconds before responding.
π₯ Simulating a 500 Server Error
|
|
Perfect for testing how your app handles failures!
Mountebank vs. WireMock
So, which one should you use?
Feature | Mountebank | WireMock |
---|---|---|
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 virtualization | Mocking 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 Idea | Summary |
---|---|
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. |
|
|