Mock API Response For Testing
Prerequisite
- You will be using Postman to demonstrate API design. Make sure you have Postman ready.
Create APIs in Postman
Once you have logged in to Postman, create or select an existing Workspace. Then, within that workspace, create a API by importing Swagger Petstore
Select Copy to collections:
Mock API Response
Create a Mock Server for the Swagger Petstore - OpenAPI 3.0
:
After successful creation, click Copy Mock URL button to copy the Mock Server URL. Then navigate to the Swagger Petstore - OpenAPI 3.0
Variables, set the value of baseUrl to the Mock Server URL and Save:
Add Examples
In the Swagger Petstore - OpenAPI 3.0
collection, add examples for the two requests and save.
Find pet by ID
In Path Variables, set the Value for id to 1
, then set the response:
{
"name": "Dog",
"photoUrls": [
"https://example.com/dog-1.jpg",
"https://example.com/dog-2.jpg"
],
"id": 1,
"category": {
"id": 1,
"name": "pets"
},
"tags": [
{
"id": 1,
"name": "friendly"
},
{
"id": 2,
"name": "smart"
}
],
"status": "available"
}
Click Save:
Click Try, and the example will correctly return the preset response.
Returns pet inventories by status
Set the response:
{
"available": 5,
"pending": 2,
"sold": 10
}
Click Save:
Click Try, and the example will correctly return the preset response.