How to Mock REST API with SOAP UI (Step by step guide)
Soap UI for simulating a Rest API??? Well, it wasn’t even the last thing that came to my mind when one of my former colleagues asked me if I could quickly create a mock up for a REST API for the application he was testing because the API was down at the moment.
By the way, here’s why you should mock a REST API,
- It enables you to stay productive while the API is being implemented.
- Mocks could be used for testing and developing the front end even when the back end is not available.
I thought it could be done in no time with Postman. I had quite a bit of experience with Postman, testing REST APIs, but soon realized it wasn’t quite as straightforward as I thought. Then I had a quick look at some other tools available for the purpose, some were way too complicated to be bothered for such a temporary quick fix and others were mostly hosted online. Couldn’t find much luck with any thing so far for my purpose and that’s when a simple google search directed me to soap ui. I already had it installed and am quite good at it, testing and mocking soap web services, so I decided to give it a go. To my surprise, it’s not only easy and fast but also quite powerful in simulating REST APIs.
Following are some of the things I tried and step by step instructions on how to.
- How to create a simple mock.
- How to mock resource paths with variables.
- How to set up multiple mock responses depending on request.
Install Soap UI if you don’t have already, and following are the steps after that.
How to create a simple mock.
1. Create a new REST project in Soap UI following the screens below.
Click the REST button on the tool bar (circled in red).
Give an appropriate URL. I have provided http://localhost:8080/test
. You can change these later.
Following is the new project that was just created (REST Project 2)
2. Create a Mock for the REST project
Right click on the project and select New REST MockService from the menu.
Give an appropriate name for the MockService
Right click on the Mock Service and select Add new mock action from the menu.
Select the HTTP method and give the resource path, it can be anything. I have selected GET
method and my resource path is test
.
3. Add a mock response
Right click on the action and select New MockResponse from the menu.
Give an appropriate name to the mock response.
Select the response content type (i.e. xml, json, text, etc…) and type in the mock response
4. Start the mock service
Right click on the MockService and select Show REST MockService Editor.
Click the green play button on the MockService Editor (circled in red), and the Mock service will start.
5. Test it out!
Send a request using the REST Project by clicking the green play button on the request.
The mock service can also be called using a browser
Mocking for resource path with variables.
Most REST URLs contain variables in the resource path, like https://example.com/users/679822 which are basically ids or names which change depending on the request. If you are wondering how to handle these, the good news is, it is already taken care of. If we just use up to users
in the resource path of the mock, whatever comes after it in the request url doesn’t really matter. This behavior is demonstrated in REST Project 3 (You can grab these project files by clicking here, and import in to Soap UI)
How to set up multiple mock responses
Want to work with multiple responses for a single path? There are two options available in Soap UI.
1. Sequence
This is easy, create as many responses as you like for the mock action. The default dispatch method is sequence.
Open up the mock action editor by double clicking on the mock action or by right click on the mock action and selecting Show Mock Action Editor. Add as many responses as you want.
The HTTP status code can also be set to preferred value in the responses. In the example below (REST Project 4) I have created 3 mock responses and one of them is an error response which returns http status 500.
See it in action
2. Script
When you want different responses depending on the request (request path or a value in the request) script option can be used.
- Create multiple responses.
- Select the script option from the dispatch drop-down for the mock action.
- Then provide the script, following is the script I’ve used.
//Following script grabs the number in the request url and appends it to the name of the response.
def requestPath = mockRequest.getPath()
log.info "Path: "+ requestPath
def id = requestPath.tokenize('/')[-1] //the id of the user comes last in the request url (eg: http://localhost:8080/user/1)
return "user"+id //this will return the response with the name 'user{id}' eg: user1
- Set a default response in case no matches found for the incoming request.
See it in action
That’s all I have come across so far with Mocking REST APIs using Soap UI.