WireMock – mock me

      No Comments on WireMock – mock me

Been using WireMock for two/three weeks now, so I don`t know a lot about it, but I want to share that I know.

Do you have third party integration? Do you have “database” and changing data does no good for your tests? Maybe you hate your REST integration and want to get rid of it (at-least for testing)?

Use WireMock!

“WireMock is a simulator for HTTP-based APIs. Some might consider it a service virtualization tool or a mock server.
It enables you to stay productive when an API you depend on doesn’t exist or isn’t complete. It supports testing of edge cases and failure modes that the real API won’t reliably produce. And because it’s fast it can reduce your build time from hours down to minutes.”

In my case integration was “slowing” me down, because I could not run tests against the same database again and again, to do that I must restore db before every test that “sends” data to db – too slow, too complicated.

WireMock have this “recording” function for like few weeks now, if not mistaken, and it saved a lot of time for me. Recording works like proxy, so I pointed our application to WireMock, told WireMock to record all requests and responses to the REST integration and run my tests – and WireMock captured it all.

So now, let`s say, I want to add new payslip and verify that my application is requesting and processing response correctly – I start WireMock with “specific” mappings, point my application to WireMock and that`s it. Now I can add as much payslips as I want and no DB or Integration is involved.

Example of “mapped” requests and what WireMock must respond to that specific URL.

{
“id” : “320289fb-0aa6-47ab-8024-301ddee3669f”,
“name” : “rest_global_agentversion”,
“request” : {
“url” : “/rest/global/agentVersion”,
“method” : “GET”
},
“response” : {
“status” : 200,
“body” : “{\”value\”:\”3.495.495.30\”}”,
“headers” : {
“Connection” : “keep-alive”,
“Content-Type” : “application/json; charset=UTF-8”,
“Organization” : “2, \”495 Testbase\””
}
},
“uuid” : “320289fb-0aa6-47ab-8024-301ddee3669f”,
“persistent” : true
}
{
“id” : “e4440ae4-f271-4e51-9a85-54f761837f86”,
“name” : “rest_tdmskapuzdbl_execute”,
“request” : {
“url” : “/rest/TdmSkApUzdBL/Execute”,
“method” : “POST”,
“bodyPatterns” : [ {
“equalToJson” : “{\r\n \”pk\”: 22,\r\n \”pkRezol\”: 1,\r\n \”notes\”: null,\r\n \”pkSask\”: 0\r\n}”,
“ignoreArrayOrder” : true,
“ignoreExtraElements” : true
} ]
},
“response” : {
“status” : 200,
“body” : “{}”,
“headers” : {
“Connection” : “keep-alive”,
“Content-Type” : “application/json; charset=UTF-8”,
“Organization” : “2, \”490 Testbase\””
}
},
“uuid” : “e4440ae4-f271-4e51-9a85-54f761837f86”,
“persistent” : true
}

Pros:

  • Tests can be re-run often without thinking about their impact on db.
  • Beside mocking, you are testing how your application works and what kind of calls it is making, so if, for example, developer changes something in call – WireMock will tell you about it.
  • You can simulate “slow” responses or bad responses e.t.c. the easy way – just edit mapping.

Cons:

  • It would be amazing IF you are not changing requests from your application to the integration – then you need to update your mocks too, because WireMock will not know how to answer to that and will pick something similar it founds and throw it in your app`s face.
  • If your application have dates in calls to the integration, keep that in mind – you will need to update your wiremock mappings every day, so your tests run – I use teamcity build configuration, that change dates in all my mocks every night, so my tests can run every day without manual intervention.
  • You can`t map all your tests in one set. WireMock have this “scenario” feature where it can respond to the same requests differently every time, but it was not working for me. (sad) Will keep testing this one.\
  • If your application is using cache for some requests – turn it off before recording. If you run your tests without cached requests, WireMock will not have response for that.
  • Turn off pinging too, if it is possible, – less noise in your mappings.

At this moment all my tests are running against WireMock, currently working on improving this “solution”.

Leave a Reply

Your email address will not be published. Required fields are marked *