Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Mock REST APIs With SOAP UI

DZone's Guide to

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.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

It'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:

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).

Create Rest project

Give an appropriate URL. I have provided http://localhost:8080/test. You can change these later.

Create Rest project

Following is the new project that was just created (REST Project 2).

Create Rest project

2. Create a Mock for the REST project.

Right-click on the project and select New REST MockService from the menu.

Create Rest project

Give an appropriate name for the MockService:

Create Rest project

Right-click on the Mock Service and select Add new mock action from the menu.

Create Rest project

Select the HTTP method and give the resource path, it can be anything. I have selected GET method and my resource path is test.

Create Rest project

Create Rest project

3. Add a mock response.

Right-click on the action and select New MockResponse from the menu.

Create Rest project

Give an appropriate name to the mock response.

Create Rest project

Select the response content type (i.e. xml, json, text, etc…) and type in the mock response.

Create Rest project

4. Start the mock service.

Right-click on the MockService and select Show REST MockService Editor.

Create Rest project

Click the green play button on the MockService Editor (circled in red), and the Mock service will start.

Create Rest project

Create Rest project

5. Test it out!

Send a request using the REST Project by clicking the green play button on the request.

Create Rest project

The mock service can also be called using a browser.

Create Rest project

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).

Create Rest project

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.

Create Rest project

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.

Create Rest project

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.

  1. Create multiple responses.
  2. Select the script option from the dispatch drop-down for the mock action.
  3. 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


  1. Set a default response in case no matches found for the incoming request.

Create Rest project

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.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
rest api ,mocking ,soap ui ,api testing ,integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}