Checking out WireMock

Our project is currently using WireMock as our mock server. From their documentation:

“WireMock is an HTTP mock server. At its core it is web server that can be primed to serve canned responses to particular requests (stubbing) and that captures incoming requests so that they can be checked later (verification).”

Due to some constraints though, I’ve been using Mokku to mock the mock API to serve my own canned responses. But of course, I was still curious about WireMock. So this good Friday, I went and checked it out. Since I was just going to have a little play around, I went for running it as a standalone process.

Step 1 was to install JDK.

Step 2 was to download the standalone jar, and then running it. Both the download link and the command to run are in the previous link for running it as a standalone process. After running it, a couple of folders get generated, one of which is the mappings folder.

Step 3 was to create a sample mapping file inside the mappings folder. After making changes, I just needed to restart by rerunning the java command. And with the sample below, I was able to get a response when I tried to access: http://localhost:8080/records.

get-records.json

{
  "request": {
    "url": "/records",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "msg": "One response to one request"
    }
  }
}

Then I tried to check out multiple responses from within the same file. For that, the needed request-response pairs were nested under a mappings array. And the results for the given example below is:

  • http://localhost/record?name=KC gets a 200 response with message “Viewing KC”.
  • http://localhost/record?name=Mario gets an error 400.
  • If I try other name values, I get a 200 response with message saying “Viewing everyone else”.

get-record.json

{
  "mappings": [
    {
      "priority": 1,
      "request":  { "url": "/record?name=KC",  "method": "GET" },
      "response": { "status": 200, "jsonBody": { "msg": "Viewing KC" } }
    },
    {
      "priority": 2,
      "request":  { "url": "/record?name=Mario",  "method": "GET" },
      "response": { "status": 400, "jsonBody": { "msg": "Error! Itsameee Mario!" } }
    },
    {
      "priority": 3,
      "request": {
        "urlPattern": "/record\\?name=.*",
        "method": "GET"
      },
      "response": { "status": 200, "jsonBody": { "msg": "Viewing everyone else" } }
    }
  ]
}

Of course, these are very simple examples but it also illustrates how it’s feasible to get started with it in under an hour or so, and that you can have this playground before pushing stuff into the mock server being used by the rest of the team.

2 thoughts on “Checking out WireMock

  1. Pingback: First 90 days in my new work as Test Manager | testkeis

  2. Pingback: Testing project lessons learned | testkeis

Leave a comment