MockServer

About

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 page, we will see how to mock a SOAP (XML) and REST (JSON) API request/response with the help of MockServer.

Setup 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
    : -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_default

Create 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
ree

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
ree

Note that MockServer has been initialised with the sample request/response files and exposed at 1090 port

Verify the Mock API's

Postman

ree
ree

Curl

curl http://localhost:1090/rest-api/books
ree
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
ree

Files are attached for the reference below.

Last updated