top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Mock Servers With Postman


In this blog have discussed what is mock server and how to create a mock server in postman. We know real servers. What is mock server then? What is the necessity of creating mock servers? Mock servers are used to help solve many different testing problems. They can help you control what inputs the UI gets, and they can

be used to help drive better designs and to better understand how an API works. In many ways the ability to create and use mock data is essential to good API testing and development.


Table Of Contents


What is Mock Server

A mock server is not a real server and it is created to act and function as a real server to verify APIs and their responses.


Need to Create a Mock Server

1. Mock server is used if we want to avoid sending requests on real time data.

One example is let us imagine an online store that uses a credit card service by calling the API. When testing that store, you might not want to send the credit card details to the credit card service, so instead create a mock server that you send these details to. Doing this allows you to test the application without needing to call the actual credit card service .

2. A Mock server is used if the APIs to be tested are still in development.


Mock Server Creation

In this section we will see how to create a mock server in postman.

To create a mock server click the mock servers field in the left side which I have marked with red color. Next click the plus symbol at the the top.


Next click create a new collection as we are designing this mock server from scratch. Under Request URL tab have entered the resource name as /getallcoursedetails. Usually these URL will be different in each environment when we run our test cases. Need not worry about the prefix of the server. Once we create a mock server a URL will be generated . The request method is selected as GET as the resource call is of GET type. Then enter the response code as 200 and enter the sample response body . This sample JSON response body will be usually created by the testers from the JSON Schema given by the developers. Sometimes developers give the sample response body also. All these details will be found in the contract document given by the developers.


Example 1 :

{{base_url}}/getallcoursedetails

{{mock_server_url}}/getallcoursedetails //getallcoursedetails is called as the resources.

sample JSON body: //This we will enter in the response body field.

{
"data":[
{
  "Course" :"Selenium",
  "Price" : "150",
  "Category" : "Java"
},
{
  "Course" :"Cypress",
  "Price" : "150",
  "Category" : "Java"
},
{
  "Course" :"Rest Assured",
  "Price" : "100",
  "Category" : "Java"
},
{
  "Course" :"Postman",
  "Price" : "50",
  "Category" : "Java Script"
}
]
}


After adding all the details click on next. Under the mock server name field can give any name. I have given Mock server demo. Then click create mock server tab.

After clicking the create mock server tab a URL will get generated. The generated URL is highlighted in red color.

Can copy the URL. This URL is the server where you have to send your request. We will get the response whatever we configured back after sending the request to the mock server created. We can start writing the automation script with dummy data and later once the APIs are ready can flip to real data.


The base URL will be constructed as https://dff84082-367f-46bc-88e4-cee0eba22d14.mock.pstmn.io/getallcoursedetails. After creating the mock server come back to the collection tab. We can see one collection created automatically. When we expand it we will have the get call which we configured before . In below screen shot we can see the mock server demo collection created. We can see the response in the response body and the status code after sending the request to the mock server.


Functional mocking Examples

We can see a default example created under the get call. This is one example what we configured before. Like this multiple examples can be configured. In postman terms its adding an example.


We are going to send another request. The resources and the JSON body will be given by the developers.

Need not go to mock servers and create one more. In existing collection itself can add a request . We might wonder then how the required status code, response body and get call will be configured. This is were the examples comes into picture.


Example 2:

{{base_url}}/getcoursedetails?name={{course}}


sample Json Body

{
  "name": "postman",
  "category": "java script",
  "students":"1232"
}

After clicking the collection named mock server demo click the three dots near it and click add request. In the add request add the URL mentioned above . How to configure the status code and the response body? Near the save button in the right corner we see three dots. After clicking three dots add example has to be clicked.



Any name can be given for the add example. I have given postman course details. Enter the JSON body and the status code. After clicking save now try calling the get course details call . Here in resource name have hard coded as postman. You can try with any names. The request call made will hit the mock server and fetch the details in the response body which we configured with the help of creating examples. Like this many examples can be created.


Conclusion

In this blog we have seen what is a mock server and what is the need to create a mock server and how it is created with few practical examples. Postman gives us an option to create mock server . You can create you own mock server and JSON body and try out in postman.

109 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page