Mountebank - Your First Service Virtualisation
In current development world, there will be scenarios were both API and its consumers are developed in parallel. Inorder to decouple their dependencies, we can mock an api response using mountebank. In this example, I will explain how to get started with your first service virtualisation using mountebank. After installing mountebank as mentioned in here (Install Mountebank), we will proceed with configuring mountebank. It can be done in few ways. The method which I explain below is by using file based configuration. This involve setting up an imposter file and a stub response
How to Create a Stub
Navigate to mountebank installation path
Create a folder and name it as “StubResponse”. ( You can name it whatever you want)
Create two json file using notepad and save it as “MockResponeForApiOne.json” and “MockResponeForApiTwo.json”( Or what ever you want).
Copy paste below code to “MockResponeForApiOne.json” . Sample example only. Update the response and predicates to suite your need ( if required)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
-
- Copy paste below code to “MockResponeForApiTwo.json” . Sample example only. Update the response and predicates to suite your need ( if required)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
How to create an Imposter
Create another file called test.json in same path as above
copy and paste below contents to it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Let us have a close look into Imposter and stubs
Responses – Contains an array of responses expected to return for the defined stub. In the above scenario the response will include status code as 200 and response body. For more info, http://www.mbtest.org/docs/api/contracts Predicates – is an array of predicates which will be used during matching process. Predicate object can be quite complex, it supports lots of different matching techniques. For more info, http://www.mbtest.org/docs/api/predicates
Let’s Mock it
Once all required files are created and saved, mountebank can be started by following command in command prompt , after navigating to installation folder of mountebank
1 2 |
|
Once mountebank is started, we can verify it by navigating to path http://localhost:2525/imposters
It will list out all active ports and a list of stubs available
Test It
Once we complete above steps, mountebank is ready with stubs. Now comes the part to test it and use. You can use any api testing tool ( Postman, soapUi etc ) for testing this. Just send the request matching the predicates and look for the responses
Below are the screenshot of Postman request
Requesting for First API.
Predicate of response One says that , request has to be of type POST, body of request should have “username” and “password” . Path of the request should have /Apitesting/v1/test?type=ResponseOne"
Now construct a postman request matching above and fire it
Request for second API
Predicate of response One says that , request has to be of type POST, body of request should have “email” and “password” . Path of the request should have /Apitesting/v1/test?type=ResponseTwo"
Now construct a postman request matching above and fire it
As you can see, both request has succesfully received expected response message
For actual development usage, just point your application to this localhost URL and start consuming virtualised API