MockServer in less than 2 minutes

For the development of applications, it can be very useful to provide an API via a local web server delivering predefined data. Especially for testing and rapid prototyping, it is highly recommended to use a mocked API instead of requesting a production system. Today I will show you how to create a local mock server...

For the development of applications, it can be very useful to provide an API via a local web server delivering predefined data. Especially for testing and rapid prototyping, it is highly recommended to use a mocked API instead of requesting a production system.

Today I will show you how to create a local mock server with a simple API in less than 2 minutes. 🚀

So open an editor and terminal of your choice, pick your stopwatch ⏰ Here we go:

Requirements

– Install Docker

– Install Docker Compose

– Docker daemon is up and running

Making it happen

Optional – Create a directory for MockServer:

mkdir /path/to/mockserver_container/
cd /path/to/mockserver_container

Create a file named docker-compose.yml :

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

Insert the service definition in docker-compose.yml:

version: "1"

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:
        - ./initializerJson.json:/config/initializerJson.json:Z

Create a file named `initializerJson`:

touch ./initializerJson.json 
vim ./initializerJson.json

Add the definitions for your request/response to `initializerJson`:

[
    {
      "httpRequest" : {
        "method" : "GET",
        "path" : "/api/v1/books",
        "queryStringParameters" : {
          "code" : ["15345"]
      }
    },
      "httpResponse" : {
        "body" : {"name": "Asterix"},
        "statusCode": 200
      }
    }
  ]

That’s all for the setup 🥳 Now let’s go and run it:

 docker-compose up

The command line should now output something like:

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 | 2023-01-02 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/books",
mockServer_1 | "queryStringParameters" : {
mockServer_1 | "code" : [ "15345" ]
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" : {"name": "Asterix"}
mockServer_1 | }
mockServer_1 | }
mockServer_1 |
mockServer_1 | 2023-01-02 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 | 2023-01-02 15:52:30 org.mockserver.log.MockServerEventLog INFO started on port: 1080

And now you can simply send a request to the mocked API:

curl localhost:1080/api/v1/books\?code\=15345

# output: {"name": "Asterix"}

The MockServer runs on port 1080 as specified in the Dockerfile. At http://localhost:1080/mockserver/dashboard you will find a simple dashboard with all requests to the server.

Easy – that’s it 🥳 

Documentation

The complete (and therefore not very intuitive) documentation can be found here.

Post a comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.