What is API Mocking?
API Mocking is nothing but an impersonation of a real API that returns pre-defined response, response code, headers, et cetera, rather than real response and parameters.
How can API Mocking be useful?
- Quick testing feedback and for early bug detection and fixing;
- Get practical insights of an API into critical implementation and usability issues with little effort;
- Parallel and non-blocking development with front-end;
- Useful in supporting DevOps by reducing dependencies.
Mocking an API with WSO2 API Manager
With the new version of WSO2 API Manager, the issue for mocking an API has been addressed quite effectively. You can deploy a new API or new version of an existing API as a prototype. This allows subscribers to test an API without subscription or monetization. This will give the scope of improvement to the API publisher.
Users were presented with an inline script that had to be manually edited for each resource. Also, the default script had to be edited to allow the resource to return a response in the developer portal.
New Scenario (with API Manager version 3.1.0):
Prototype implementation is equipped with mock payload generation.
If the API definition contains mock responses for more than one response code per-resource, the payloads will be presented as multiple payloads but the “mc.setPayloadJson or mc.setPayloadXML” will only contain the lowest response code value. Furthermore, inline scripts of each resource can be further edited by users to suit the required outcome better.
Once the generated mock payload scripts have been saved, the API can be deployed as a prototype and tested in the developer portal.
There are two ways to deploy an API prototype.
1. Create a new prototype API :
The new API can be created by following the steps as mentioned in the WSO2 API Manager documentation.
Once created, the API can be deployed as prototype with 2 options:
- Prototype Endpoint: You need to provide an endpoint to a prototype API. Once deployed as prototype this API can be tested in Developer Portal from the WSO2 API Manager.
- Prototype Implementation: The inline script will be automatically generated for each response code and payload type (JSON and/or XML) according to the response body example values or response schemas defined in the API definition.
2. Create a prototype API as new version from an existing API:
The new version for a given API can be created by following the steps as mentioned in WSO2 API Manager documentation.
Once created, the API can be deployed in either way as mentioned here.
The prototype API created by any of the mentioned methods can be tested by logging into Developer Portal from WSO2 API Manager.
Note: the prototype API can be tested directly without subscription as subscriptions are not allowed for prototyped API.
Once the API prototype is tested thoroughly and the final version from the prototyped API is ready for release in the publisher, it’s time for subscription and monetization. This can be achieved by publishing the prototyped API as in Step 2.
As more and more businesses are moving towards APIs (the age of API is upon us), the API mocking (prototyping) has made the life of a developer much easier.