Most developers have been in a situation where they need to make mock API calls. Whether you need to mock a call to help your team implement new features or you need a mock server for integration testing, mountebank is the answer.
Mountebank is an open-source tool that allows developers to create multiple endpoints with several special conditions so that they can mock real API calls to test applications. Today, we'll describe how mountebank works and guide you through a simple tutorial.
How Mountebank Works
Simply stated, mountebank allows you to create imposters that contain all server information. You can set the connection type and the port you want to use. Inside imposters, you can also set stubs. A stub is like one endpoint where you can set your predicate to inform mountebank of the kind of call you expect, as well as the response mountebank must return when an added element "matches" a request.
You can set imposters and stubs by writing code using EJS files (Embedded Javascript Templates), or--what's truly remarkable--by using a mountebank API. In other words, you can actually use the mock server you created to post your new imposters and stubs, allowing you to create dynamic tests!
Mountebank in Action
Now that we've examined mountebank in theory, we'll provide a quick tutorial so that you can begin to use it on your own. First of all, you will need to install Node.js to use mountebank. After installation is complete, run the follow command:
$ npm install -g mountebank
Then run:
$ mb
Now you have an instance of mountebank running on your computer. You can test it by accessing mountebank documentation.
Creating Your First Imposters and Stubs
First, let's create an imposter.ejs file by writing this simple code:
By creating this code, we're creating one imposter that will listen on port 1,000 only through a https protocol. In this example, we're creating only one stub. Since this is an array, however, you can create as many stubs as you like for this port, which creates multiple scenarios for potentially successful and unsuccessful cases.
Next, we run the following command:
$ sudo mb --configfile imposters.ejs
We can now make a GET to https://localhost:1000/tutorial, and we'll get the response we set.
Posting and Deleting Imposters to the Mountebank API
As mentioned above, it's possible to create imposters by posting to the server API. In this case, all you have to do is make a POST to http://localhost:2525/imposters and use as your data the same imposter we created above. (Note that you can't update your imposter on mountebank; you only have the option to create or delete.)
To delete an imposter, simply make a DELETE to http://localhost:2525/imposters/1000, remembering to replace the last number with the port you are using. The operations enable you to dynamically create imposters before an integration test. All you need to do is configure the stub, make the calls, and delete after use.
Complex Predicates
The predicate we chose as our example is a very simple one, but you can use as many conditionals as you need to create your predicates. Mountebank accepts several operators, such as equals, contains, matches, not, or, and, etc. You can combine these operators with regex and create powerful predicates that will provide a response to every call.
Let's walk through an example of a more elaborate predicate:
In this example, we'll telling mountebank that it should expect requests on any of those 3 paths with a POST method and a Content-Type header. We'll also tell it to expect a body. If one call satisfies all these items, mountebank will return the desired response. (You may view more examples of predicates here.)
Using Mountebank on Your Own
If you enjoyed today's brief overview of mountebank and want to learn more about it, read the examples on mountebank's website. These examples will help you to learn and develop your own mock calls to make the development process easier for you and your team!Author
Ricardo Ribeiro
Ricardo Ribeiro is a Java Engineer at Avenue Code. He has been a developer since 1998 and is passionate about coding.