Mocking Made Easy: Using MockServer for REST and SOAP API Testing
- pranaypourkar
- May 1, 2023
- 2 min read
Updated: Jul 11, 2023
MockServer is a powerful open-source HTTP/HTTPS mock server that supports mocking both SOAP and REST requests and responses. With advanced features like request matching, response templating, proxying, and SSL/TLS support, MockServer provides a flexible and robust solution for API testing. Additional features like header matching, fault injection, and dynamic responses further enhance the tool's capabilities. MockServer also offers client libraries for several programming languages, allowing for easy integration with testing frameworks like JUnit. Finally, Testcontainers provides seamless support for MockServer as an HTTP mock server, making it a popular choice for modern testing environments
For more details, visit the official site https://www.mock-server.com/
In this blog, we will see how to mock a SOAP (XML) and REST (JSON) API request/response with the help of MockServer.
Let's start with setting up MockServer.
We will use docker-compose way of starting the MockServer. Create a file named docker-compose.yml and add the below contents to it.
version: "3.9"
# https://docs.docker.com/compose/compose-file/
services:
mockserver:
container_name: mockserver
image: mockserver/mockserver:5.14.0
#command: -logLevel DEBUG -serverPort 1090
environment:
MOCKSERVER_LOG_LEVEL: INFO
MOCKSERVER_SERVER_PORT: 1090
MOCKSERVER_INITIALIZATION_JSON_PATH: /mockserver/stubs/*.json
ports:
- "1090:1090"
volumes:
- ./stubs:/mockserver/stubs
networks:
default:
name: company_defaultCreate a folder with name stubs, in which we will store sample json files for initialisation.
Let's create mock request/response file (REST-books-list.json) for sample REST API inside the stubs folder.
{
"httpRequest": {
"method": "GET",
"path": "/rest-api/books"
},
"httpResponse": {
"statusCode": 200,
"body": [{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"id": "10001"
},
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"id": "10002"
},
{
"title": "Pride and Prejudice",
"author": "Jane Austen",
"id": "10003"
}]
}
}Let's create mock request/response file (SOAP-books-list.json) for sample SOAP API inside the stubs folder.
{
"httpRequest": {
"method": "POST",
"path": "/soap-api/books",
"body": {
"type": "XML",
"xml": "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetBooksRequest xmlns=\"http://example.com/\"></GetBooksRequest></soap:Body></soap:Envelope>"
}
},
"httpResponse": {
"statusCode": 200,
"headers": {
"Content-Type": "application/xml"
},
"body": {
"type": "XML",
"xml": "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetBooksResponse xmlns=\"http://example.com/\"><Book><ID>10001</ID><Title>To Kill a Mockingbird</Title><Author>Harper Lee</Author></Book><Book><ID>10002</ID><Title>The Great Gatsby</Title><Author>F. Scott Fitzgerald</Author></Book><Book><ID>10003</ID><Title>Pride and Prejudice</Title><Author>Jane Austen</Author></Book></GetBooksResponse></soap:Body></soap:Envelope>"
}
}
}Let's start the MockServer with the help of below command
docker-compose up mockserver
Access the MockServer UI page. Go to below url to view the UI which shows logs, active expectations and requests.
http://localhost:1090/mockserver/dashboard
Note that MockServer has been initialised with the sample request/response files and exposed at 1090 port
Access the Mock API's via Postman or using Curl Command.
Postman


Curl
curl http://localhost:1090/rest-api/books
curl -X POST -H "Content-Type: application/xml" -d '<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetBooksRequest xmlns="http://example.com/"></GetBooksRequest></soap:Body></soap:Envelope>' http://localhost:1090/soap-api/books
Files are attached for the reference below.
Thank you for taking the time to read this post. I hope that you found it informative and useful in your own development work.








Comments