docker,  automation,  testing

Setting up MockServer

Using MockServer to test in isolation.

Setting up MockServer

Main goals:

  • Simple and declarative testing environment setup.
  • Runs everywhere

Strategy

  • Set up a replicable local server.

    Docker - Containerized applications (actually write once, runs anywhere)

  • Provide an expectations json where all the requests are mapped out.

    MockServer - Create local server with complex request/response expectations through a json resource or a REST API.

    MockServer

Making it happen

  • Install Docker.

  • Install Docker Compose.

  • Make sure your docker daemon is up and running.

  • Create a folder for your MockServer setup.

    $ mkdir /path/to/mockserver_container/ && cd /path/to/mockserver_container
  • Create the docker-compose.yml .

    • This is where you’ll setup your container for MockServer.
    $ touch ./docker-compose.yml && nano ./docker-compose.yml

    Paste this code snippet:

    version: '2.4'
    services:
    mockServer:
    image: mockserver/mockserver:latest
    ports:
  • 1080:1080 environment: MOCKSERVER_PROPERTY_FILE: /config/mockserver.properties MOCKSERVER_INITIALIZATION_JSON_PATH: /config/initializerJson.json volumes:

  • type: bind source: . target: /config

    
    This is saying:
    
    - Create a service named `mockserver` using the docker image from `mockserver/mockserver:latest`.
    
    - Assign the `host port` 1080 to the `container port` 1080.
    - Set the `enviroment variables` to the provided paths.
    
    - Create a `volume` binding from the `current directory` (this file's dir) to `/config` inside the container.
    
  • Setup the Expectation Initializer JSON.

    • Create the initializerJson
    $ touch ./initializerJson.json && nano ./initializerJson.json
  • Paste this code snippet:

    [
      {
        "httpRequest": {
          "method": "GET",
          "path": "/api/v1/weather",
          "queryStringParameters": {
            "code": ["10969"]
          }
        },
        "httpResponse": {
          "body": "{\"wheather\": 20.5}",
          "statusCode": 200
        }
      }
    ]

Full documentation reference can be found here

Running your container

Now that we’ve got the plumbing out of the way, it’s time to run it.

  • Before running your container check if have any other containers in your environment:

    $ docker ps -a

    This should print an empty table.

  • Build your container:

  • Inside the folder you created the .yml type:

$ docker-compose up

Your terminal should print something like this:

Creating mockserver_mockServer_1 ... done
Attaching to mockserver_mockServer_1
mockServer_1  |
mockServer_1  | java  -Dfile.encoding=UTF-8 -jar /opt/mockserver/mockserver-netty-jar-with-dependencies.jar  -server
mockServer_1  |
mockServer_1  | 2020-04-11 15:52:30  org.mockserver.log.MockServerEventLog  INFO  creating expectation:
mockServer_1  |
mockServer_1  |   {
mockServer_1  |     "id" : "3f3962d2-9c03-4dce-95cd-2522962ceccb",
mockServer_1  |     "priority" : 0,
mockServer_1  |     "httpRequest" : {
mockServer_1  |       "method" : "GET",
mockServer_1  |       "path" : "/api/v1/weather",
mockServer_1  |       "queryStringParameters" : {
mockServer_1  |         "code" : [ "10969" ]
mockServer_1  |       }
mockServer_1  |     },
mockServer_1  |     "times" : {
mockServer_1  |       "unlimited" : true
mockServer_1  |     },
mockServer_1  |     "timeToLive" : {
mockServer_1  |       "unlimited" : true
mockServer_1  |     },
mockServer_1  |     "httpResponse" : {
mockServer_1  |       "statusCode" : 200,
mockServer_1  |       "body" : "{\"wheather\": 20.5}"
mockServer_1  |     }
mockServer_1  |   }
mockServer_1  |
mockServer_1  | 2020-04-11 15:52:30  org.mockserver.cli.Main  INFO  logger level is INFO, change using:
mockServer_1  |  - 'ConfigurationProperties.logLevel(String level)' in Java code,
mockServer_1  |  - '-logLevel' command line argument,
mockServer_1  |  - 'mockserver.logLevel' JVM system property or,
mockServer_1  |  - 'mockserver.logLevel' property value in 'mockserver.properties'
mockServer_1  | 2020-04-11 15:52:30  org.mockserver.log.MockServerEventLog  INFO  started on port: 1080

Sending requests

Open a new terminal window and enter:

To see info about your available containers enter again:

$ docker ps -a

You can now see some info about your running container.

$ curl localhost:1080/api/v1/weather\?code\=10969

You should see the response:

{"wheather": 20.5}

And thats it!

You’ve got yourself a local server where you can mock simple and complex requests and responses.

Useful links:

REST API Swagger docs

...