DEV Community

Cover image for Setting up MockServer
Iury Souza
Iury Souza

Posted on • Edited on

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 - Creates a local server with complex request/response expectations through a json resource or a REST API.

image

Making it happen



  $ mkdir /path/to/mockserver_container/ && cd /path/to/mockserver_container


Enter fullscreen mode Exit fullscreen mode
  • Create the docker-compose.yml .

    • This is where you'll setup your container for MockServer.


  $ touch ./docker-compose.yml && vim ./docker-compose.yml


Enter fullscreen mode Exit fullscreen mode

Paste this code snippet:



version: "2.4"
services:
  mockServer:
      image: mockserver/mockserver:latest
      ports:
        - 1080:1080
      environment:
        MOCKSERVER_WATCH_INITIALIZATION_JSON: "true"
        MOCKSERVER_PROPERTY_FILE: /config/mockserver.properties
        MOCKSERVER_INITIALIZATION_JSON_PATH: /config/initializerJson.json
      volumes:
        - type: bind
          source: .
          target: /config



Enter fullscreen mode Exit fullscreen mode

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 && vim ./initializerJson.json


Enter fullscreen mode Exit fullscreen mode
  • Paste this code snippet:


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



Enter fullscreen mode Exit fullscreen mode

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.

  • Inside the folder you created the .yml type:


  $ docker-compose up


Enter fullscreen mode Exit fullscreen mode

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 




Enter fullscreen mode Exit fullscreen mode

Sending requests

Open a new terminal window and enter:

To see info about your available containers enter again:



$ docker ps -a


Enter fullscreen mode Exit fullscreen mode

You can now see some info about your running container.



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


Enter fullscreen mode Exit fullscreen mode

You should see the response:



{"wheather": 20.5}


Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)