Search This Blog

Sunday, September 15, 2019

Mocking a RESTful Microservice in MounteBank

What is MounteBank?

Mountebank is the first open source tool to provide cross-platform, multi-protocol test doubles over the wire. Simply point your application under test to mountebank instead of the real dependency, and test like you would with traditional stubs and mocks.
Basically it provides a server that can be configured using a DSL to simulate requests and responses over variety of protocols (http, https, tcp and smtp).

How it works?

MounteBank

Installation

There are various ways to install MounteBank on different platforms. We recommend to use via npm.
  1. Install npm
  2. Install MounteBank globally
$ npm mountebanck -g

Start MounteBank

$ mb –allowCORS –allowInjection –mock
By default it starts on the port 2525.
Now let’s learn some basic terminologies used by MounteBank DSL.
  • Response - Defines status code, headers and body
  • Predicate - Conditions to check request to match some criteria based on which response will be returned
  • Stub - Also called Imposter is a Collection of predicates and responses for simulating an API
The DSL is very rich and we cannot cover everything here. For more details visit the API contract.
Now let us mock Get All Posts and Get a Specific Post API.
Use this JSON and send it to MounteBank Server already running on your local machine.

MounteBank Server
Create an Impostor for Get All Posts

It should create an impostor successfully and return same JSON as you sent in request.
Now go and hit http://localhost:9999/posts in REST Client or Browser and you should receive JSON having two posts data.

Response from GET /posts
Response from GET /posts

Response from POST /posts
Response from POST /posts

Now it’s your turn. Create impostors for rest of the RESTful APIs and get hands on it.

My Profile

My photo
can be reached at 09916017317