Tuesday, May 29, 2018

WireMock to mock APIs

Full Stack Software Development Blog


If you have a need to mock APIs then WireMock to the rescue.

See http://wiremock.org/

In the last blog I have defined APIs in swagger hub. Now I need a way to exercise those APIs. I'm very glad to have found WireMock.

I followed the getting started guide to run WireMock locally (http://wiremock.org/docs/getting-started/). It must be a Spring Boot jar :)

I downloaded the standalone jar and ran it on port 9999.

  • java -jar wiremock-standalone-2.18.0.jar --port 9999


I then added a stub API

  • curl -X POST http://localhost:9999/__admin/mappings -d '{ "request": { "method": "GET", "url": "/hello1" }, "response": { "body": "Hello world!" }}'

Exercise the stub

  • curl http://localhost:9999/hello1

Added a new stub to return a json body

  • curl -X POST http://localhost:9999/__admin/mappings -d '{ "request": { "method": "GET", "url": "/hello2" }, "response": { "jsonBody": {"Hello": "world!" }}}'

Exercise the stub

  • curl http://localhost:9999/hello2

To see all stub mappings

  • curl http://localhost:9999/__admin/

See http://wiremock.org/docs/stubbing/ for more ways to stub.

Shutdown the local server

  • curl -X POST http://localhost:9999/__admin/shutdown


Next I deployed the WireMock server to Heroku. You can too. See my github repo
https://github.com/gpratte/wiremock-deploy-heroku

No comments:

Post a Comment