Integration tests - Mocking your HTTP endpoints without coding

May 25, 2019

mockingjay server

Finally everyone understood the value of the old Unix philosophy:

Make each software component do one thing well. To do a new job, build afresh rather than complicate old programs by adding new “features”.

Adopting the microservices pattern with heavy investment in PaaS/IaaS you want to be able to quickly iterate over small services and deploy to live quickly - without fear of breaking things.

If you are using this kind of architecture you will be faced with the challenge of ensuring that your huge numbers of services can actually talk to each other.

Mockingjay is the perfect tool to test your HTTP integration endpoints.

  • Consumer driven contracts (CDCs) to run against your real downstream services
  • no coding whatsoever
    • even non developers can add new scenarios to test with
  • entirely language agnostic
    • if you speak HTTP you can use mockingjay.
  • checks the structure of the data (JSON and XML only)

Install mockingjay as Docker Image

docker run --rm=true -p 8080:8080 -v ${PWD}/example.yaml:/example.yaml \
   quii/mockingjay-server:1.10.7 -config=/example.yaml -port=8080
  • works really nicely with docker compose
fakeAPI:
  image: quii/mockingjay-server:1.10.7
  volumes:
    - ./test/fakes:/fakes
  command: -config=/fakes/api.yaml
  ports:
   - "9090:9090"

Install mockingjay as binary

https://github.com/quii/mockingjay-server/releases

STEP 1 - Define your endpoints

We can create a working server just from a yaml configuration - let’s call it xample.yaml:

- name: Test endpoint 1
  request:
    uri: /hello
    method: GET
  response:
    code: 200
    body: '{"message": "hello, World"}'   # * matches any body
    headers:
      content-type: application/json

- name: Failing endpoint
  request:
    uri: /card
    method: POST
    body: Greetings
  response:
    code: 500
    body: Oh bugger

- name: Posting forms
  request:
    uri: /cats
    method: POST
    form:
      age: "10"
      name: Hudson
  response:
    code: 201
    body: Created

# define as many as you need...

STEP 2 - Start mockingjay-server

mockingjay-server -config=example.yaml 
mocking-jay: 2019/05/25 13:38:32 Listening on port 9090

STEP 3 - Test your endpoints:

  • GET /hello
curl -D - http://localhost:9090/hello

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 25 May 2019 11:24:58 GMT
Content-Length: 27

{"message": "hello, World"}
  • POST /card
curl  -X POST --data "Greetings" -H "Content-Type: text/plain" -D - http://localhost:9090/card

HTTP/1.1 500 Internal Server Error
Date: Sat, 25 May 2019 11:10:37 GMT
Content-Length: 9
Content-Type: text/plain; charset=utf-8

Oh bugger
  • POST /cats
curl  -X POST --data "age=10&name=Jerry" -D - http://localhost:9090/card

HTTP/1.1 201 Created
Date: Sat, 25 May 2019 11:41:08 GMT
Content-Length: 7
Content-Type: text/plain; charset=utf-8

Created