How to Mock REST APIs With SOAP UI
This tutorial shows how to mock a REST API using SOAP UI, with screenshots included, for testing and developing your front end if the back end isn't available.
Join the DZone community and get the full member experience.
Join For FreeIt's super quick and easy to mock or simulate any REST API with SOAP UI and this post shows you how to do it with step by step instructions and screenshots.
Mocking your REST APIs 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.
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 mockup for a REST API for the application he was testing because the API was down at the moment.
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 with for such a temporary quick fix and others were mostly hosted online. I didn't have much luck with anything 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:
- Creating a simple mock - Step by step instructions on how to simulate a REST API.
- Mocking for resource paths with variables - How Soap UI works for path variables.
- Multiple mock responses depending on request - How to use the script dispatcher to match a response based on the request.
Install Soap UI if you don’t have already, and following are the steps after that.
Creating a Simple Mock
1. Create a new REST project in Soap UI following the screens below.
Click the REST button on the toolbar (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 using the form at the bottom of the post, and import into Soap UI).
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 a 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.
Which tools have you used for simulating REST APIs? Any suggestions? I would love to hear from you, so please leave a comment below.
Click here to download the examples used in this post as project files, which you can import to Soap UI.
Published at DZone with permission of Dilini Rajapaksha. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments