Mock SOAP or REST API for testing
API testing is critical to software development, ensuring your application interacts seamlessly with external services. However, relying on real APIs during development and testing can present challenges like unpredictability, rate limits, and associated costs. This is where mocking API responses becomes invaluable. In this comprehensive guide, we will explore how to mock API responses effectively using SoapUI, allowing you to simulate server behavior and test your application under various scenarios.
Table of contents
- Understanding mocking API responses
- Why mocking APIs matters?
- Getting started with SoapUI
- Mocking API responses in SoapUI
- Advanced API mocking techniques in SoapUI
- Mocking SOAP APIs vs. Mocking REST APIs
- API mocking best practices
- API mocking considerations
- Practical applications of API response mocking
- Conclusion
Understanding mocking API responses

Mocking API responses is creating simulated or imitation responses from an API (Application Programming Interface) without making actual requests to an external service. This technique allows developers and testers to replicate the behavior of a real API, including both expected and exceptional responses, in a controlled and predictable manner. Mocking API responses is commonly used to isolate applications from external dependencies, simulate various scenarios, and efficiently conduct testing during the software development lifecycle.
Why mocking APIs matters?
Before we delve into the intricacies of mocking API responses, let us understand why it is crucial in the software development lifecycle.
- Isolation: Mocking enables you to isolate your application from external dependencies, ensuring that your tests remain consistent, irrespective of the external API’s status or availability.
- Versatility: You can simulate various scenarios, including success, error, and edge cases, which may be challenging to trigger using the actual API.
- Efficiency: Mocked responses are fast and do not consume actual API resources, making your testing process more efficient and cost-effective.
- Parallel Development: Developers can work independently on different application parts, relying on mock data to simulate API interactions.
- Cost Savings: Reducing reliance on third-party APIs can lead to significant cost savings, especially when those APIs have usage fees.
Now that we understand the benefits let us dive into the practical aspects of mocking API responses using SoapUI.
Getting started with SoapUI
Before you can start mocking API responses, you must set up SoapUI. Follow these steps to get started:
Step 1: Install SoapUI
Download and install SoapUI from the official website if you haven’t already. Otherwise, kindly refer to our article for a detailed overview of the installation of SoapUI on your respective OS.
Step 2: Create a SOAP project
Once you have SoapUI installed, follow these steps to create a SOAP project:
- Launch SoapUI.

- Click on the File menu and select New SOAP Project.

- Provide a name for your project and the WSDL URL or local file if you have one.

- Click OK to create the project.

With SoapUI set up, you can start mocking API responses.
Mocking API responses in SoapUI
SoapUI offers powerful tools for creating mock services and defining their behaviors. Follow these steps to mock API responses effectively:
Step 1: Create a mock service
The first step is to create a Mock Service in your SoapUI project:
- In your SoapUI project, right-click on your project name in the navigator window.

- Select New MockService.

- Provide a name for your MockService and click OK.

Step 2: Configure the mock service
Now, you will need to configure your Mock Service to specify how it should behave when it receives requests:
- Under your project, under the Navigator window, expand the MockServices section. You will see your newly created Mock Service. Right-click on it and select Show MockService Editor.

- In the MockService Editor window, navigate to the top of the tab.
- This is where you define how the mock service should respond to incoming requests. You can add multiple responses, each associated with specific requests.
- Click the Creates a new MockOperation for this MockService button to add a response.

- Configure the response by setting the headers and response content. You can use XML, JSON, or any other format suitable for your API.

- Save your changes.
Step 3: Start the mock service
Now that you have configured your Mock Service, it is time to start it so that it can receive and respond to requests:
- Right-click on the Mock Service in the Navigator window and select Open Request.

- SoapUI will provide a URL where your mock service is now accessible. You can use this URL in your application or testing tools to interact with the mock API.

Step 4: Test the mocked API
With your Mock Service up and running, you can now send requests to it as if it were the actual API. Testing various scenarios, including success and error cases, is crucial to ensure your application handles them correctly. This comprehensive testing approach will help identify potential issues and ensure that your application is robust and capable of providing a smooth user experience.
Advanced API mocking techniques in SoapUI
While the basic steps above cover the fundamentals of mocking API responses in SoapUI, there are advanced techniques that can make your mock services even more powerful and flexible:
Dynamic responses
SoapUI allows you to create dynamic responses using scripts. This is particularly useful when you need to simulate changing data or complex behavior:
- In the MockService Editor, generate a response as usual.

- In the response editor, go to the Script tab.

- Write a script (e.g., Groovy script) that causes the response content dynamically based on the request parameters.
// Groovy script for dynamic response generation
import com.eviware.soapui.support.XmlHolder
// Extract the value of a parameter from the request
def request = new XmlHolder(mockRequest.getRequestContent())
def parameterValue = request.getNodeValue("//parameterName")
// Define the response content
def response = mockResponse.getResponseContent()
// Replace a placeholder in the response content with the extracted parameter value
response = response.replace("{{dynamicValue}}", parameterValue)
return response

This script allows you to tailor the response content dynamically based on the specific parameters or data in the incoming request. It is helpful for simulating scenarios where the API’s responses depend on the data provided in the requests.
Conditional responses
You can define conditions for when a particular response should be triggered. For instance, you might want to return an error response when a specific query parameter is present:
- In the MockService Editor, create multiple responses.
- Define conditions based on request properties such as headers, query parameters, or content in each response.
- The first response whose needs match the incoming request will be sent.
Scripted delays
To simulate real-world server behavior, you can introduce delays in your mock responses:
- In the MockService Editor, edit a response by right-clicking on your response and selecting Creates a new MockOperation for this MockService.
- In the response editor, click the Script tab.
- Use a script to add a delay before sending the response, simulating network latency.
// Groovy script for introducing a scripted delay
import java.util.concurrent.TimeUnit
// Define the duration of the delay in milliseconds (e.g., 1000 milliseconds = 1 second)
def delayMillis = 1000
// Introduce the delay
TimeUnit.MILLISECONDS.sleep(delayMillis)

In this script, you can specify the duration of the delay in milliseconds. This allows you to mimic scenarios where responses from the actual API may take some time due to network latency. It’s a valuable tool for testing your application’s responsiveness and handling of delayed responses.
Mocking SOAP APIs vs. mocking REST APIs
Mocking SOAP APIs in SoapUI
- WSDL-Based Mocking: SOAP APIs often use Web Services Description Language (WSDL) definitions to describe their structure and operations. SoapUI excels in handling WSDL-based mocking, allowing you to import WSDL files directly and generate mock services that adhere to the specified contract. This precise modeling ensures that your SOAP mock services closely mimic the API.
- SOAP Envelopes: SOAP requests and responses are typically structured within SOAP envelopes. SoapUI provides dedicated support for constructing and parsing SOAP envelopes, making it straightforward to define and modify the content of SOAP messages in your mock responses.
- Headers and Security: SOAP APIs commonly involve various headers for authentication, security, and other purposes. With SoapUI, you can easily customize headers and implement security protocols like WS-Security when mocking SOAP APIs, ensuring comprehensive testing of these aspects.
Mocking REST APIs in SoapUI
- Resource-Oriented Design: RESTful APIs are centered around resources and their representations. SoapUI allows you to define and structure resources when mocking REST APIs, making it easy to create endpoints that mimic the structure of the actual API.
- HTTP Methods: REST APIs use HTTP methods like GET, POST, PUT, and DELETE to interact with resources. SoapUI provides intuitive support for configuring these methods and their corresponding responses, allowing you to simulate a wide range of RESTful interactions.
- Media Types and Content Negotiation: REST APIs often involve content negotiation and support multiple media types (e.g., JSON, XML). SoapUI enables you to specify the media type in your mock responses, ensuring that your REST mocks align with the expected data formats.
- Query Parameters and Path Variables: In REST, query parameters and path variables are standard mechanisms for passing data to the API. SoapUI allows you to define and manipulate these parameters in your mock services, facilitating the testing of various query scenarios.
API Mocking best practices
To effectively use mock API responses in SoapUI, consider these best practices:
- Organize Your MockServices: If your application interacts with multiple external APIs, create separate mock services for each to maintain clarity and organization.
- Keep Responses Realistic: While mocking is useful, ensure your mock responses closely mimic the actual API’s behavior.
- Version Control: If your mock services become complex or critical to your development process, consider versioning them to track changes and collaborate effectively with your team.
- Documentation: Document your mock services’ behavior comprehensively and share this documentation with your team members and testers. This ensures everyone is on the same page regarding the mock service’s behavior.
- Update as APIs Evolve: As the real APIs you are simulating evolve, update your mock responses accordingly to maintain accuracy.
API mocking considerations
In addition to the core concepts discussed, there are further considerations when it comes to mocking API responses in SoapUI:
Security testing
Mocking responses are not limited to simulating successful interactions. You can also use it to test security vulnerabilities. Creating mock responses that affect security breaches allows you to assess how well your application detects and responds to threats.
Load testing
SoapUI also provides load-testing capabilities. You can simulate a high volume of requests to your mock services to evaluate how your application handles heavy loads. This helps you identify potential bottlenecks and optimize your system for scalability.
Integration with CI/CD
Consider integrating SoapUI with your CI/CD pipeline to streamline your development and testing process. This allows you to automatically run API tests, including those using mock services, whenever changes are made to your codebase.
Practical applications of API response mocking

Let us explore some real-world applications and use cases to understand further the significance of mocking API responses in SoapUI.
Use Case 1: Rapid development
Imagine you are developing a mobile app that relies on a third-party weather API to display real-time weather data. However, the API has rate limits and may not always be available during development and testing. By creating a mock service in SoapUI that mimics the weather API, your development team can work uninterrupted, even if the API experiences downtime or rate-limiting.
Use Case 2: Testing error handling
One crucial aspect of application development is error handling. With SoapUI, you can easily simulate error responses from external APIs to ensure that your application gracefully handles unexpected situations. This is particularly valuable for e-commerce platforms, where payment gateways and inventory systems occasionally return errors.
Use Case 3: Simulating complex scenarios
Sometimes, your application may need to interact with complex APIs that exhibit intricate behavior, such as multi-step authentication processes or cascading requests. With SoapUI’s scripting capabilities, you can create mock services that simulate these complex scenarios, allowing your developers and testers to ensure your application handles them seamlessly.
Final thoughts on how to mock APIs with SoapUI
In software development, testing methodologies are essential. Mocking API responses in SoapUI is a crucial strategy for reliability. It gives flexibility and predictability in testing, and SoapUI offers features for precise simulations. Every mock response, dynamic behavior, and condition enhances software reliability.
Get started with SoapUI and take your API testing to the next level.
Follow our blog
Be the first to know when we publish new content.
Related API articles
- Introduction to API testing
- REST vs SOAP APIs
- SoapUI security testing
- Postman API design and development
- Postman API security
I tried to install Soap UI but I’m getting some errors. Please help!
Hi Ganga,
Please tell us what type of error(s) you’re seeing.