Mock a REST Service using SOAPUI with JSON response
Sometimes, backend service is not ready to receive requests and we may want to mock the service. In such cases we have sample request and response but integration cannot be tested unless we actually hit the service. Mocking the services is a good option to continue and progress.
In this tutorial, we will use SOAP UI to mock a REST service with POST method and JSON response(static in nature). This is the basic tutorial to configure static response with no request validation. We can use the SOAP UI to test the created mock service also before, we try to hit from any Integration service.
Sample URL(for actual service): http://localhost:8080/api/test
Sample JSON Response:
{
"name": "John",
"age": 30,
"car": null,
"regitered": true
}
Sample JSON Response:
{
"name": "John",
"age": 30,
"car": null,
"regitered": true
}
Steps:
1. Open SOAP UI and click on REST to create a New REST Project.
2. Provide the url for the api you want to create a mock service.
3. Once the project is created, right click on host name(http://locatlhost:8080) under REST Project 1 and then click on Generate REST Mock Service.
4. Specify the name for mock service and click on OK.
5. A window will open showing that mock service GET /api/test has been started at port 8089.
6. Click on stop button to stop the mock service. Click on /api/test under REST MockService 1. Change method from GET to POST.
7. Mock response is specified as Response1 which was automatically created. Double click Response 1 from this window or navigator window.
8. Keep HTTP Status Code to be 200 - OK, if your back end application sends the status code 200 else change to the code which backend application sends. Update the sample response under content window editor. Specify media type to application/json.
9. Start the mock service. You will see the mock service POST /api/test is started at port 8089.
10. To test this service, open the request under REST Project 1. Update the hostname:port with the value for mock service. Here in this case will be http://localhost:8089. Click on run.
You will see the response is received. And also we can see in logs that request triggered and is successful.
Note: In SOAP UI, we can also configure dynamic response using groovy script.
Well explained !
ReplyDeleteThank You :)
DeleteAnother important article !!
ReplyDeleteThank you :)
Delete