DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • MuleSoft Integrate With ServiceNow
  • Demystifying APIs for Product Managers
  • How to Seamlessly Integrate Data Into NetSuite
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java

Trending

  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Fixing Common Oracle Database Problems
  • Integrating Security as Code: A Necessity for DevSecOps
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Mock REST APIs With SOAP UI

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.

By 
Dilini Rajapaksha user avatar
Dilini Rajapaksha
·
Dec. 01, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
61.0K Views

Join the DZone community and get the full member experience.

Join For Free

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:

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

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.

REST Web Protocols SOAP

Published at DZone with permission of Dilini Rajapaksha. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft Integrate With ServiceNow
  • Demystifying APIs for Product Managers
  • How to Seamlessly Integrate Data Into NetSuite
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!