{"id":228,"date":"2018-02-27T11:41:49","date_gmt":"2018-02-27T11:41:49","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=228"},"modified":"2022-03-04T11:46:50","modified_gmt":"2022-03-04T11:46:50","slug":"mocking-apis-using-wiremock-for-testing-negative-cases-when-it-is-not-possible-to-change-the-api-behaviour","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2018\/02\/mocking-apis-using-wiremock-for-testing-negative-cases-when-it-is-not-possible-to-change-the-api-behaviour","title":{"rendered":"Mocking APIs using WireMock for testing negative cases when it is not possible to change the API behaviour"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\"><!-- pn-tldr --><\/p>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>WireMock lets you produce failures a real API will not give you on demand: timeouts, 500s, malformed bodies.<\/li>\n<li>Negative paths are where most production incidents live and where coverage is usually thinnest.<\/li>\n<li>Pin stubs to the real contract so a provider change breaks the test rather than production.<\/li>\n<\/ul>\n<p><!-- \/pn-tldr --><\/p>\n<h3>Why do we need to mock APIs ?<\/h3>\n<p>A lot of times when you develop integration tests there is a need to mock different APIs to test for negative cases to check how the rest of the platform behaves in that case and it is not feasible to change the API to return errors. This is especially true when you are trying to run the tests against a staging environment which simulates exactly the Production environment.<\/p>\n<p>In these cases we can use WireMock to mock different APIs and to make the API return exactly what response we need for our tests.<br \/>\nBelow we are going to run WireMock as a standalone server not as a Gradle or Maven dependency.<\/p>\n<pre>How to install WireMock ?\n1. Browse to :\nhttp:\/\/wiremock.org\/docs\/running-standalone\/<\/pre>\n<pre>2. Download the standalone jar from :\nhttp:\/\/repo1.maven.org\/maven2\/com\/github\/tomakehurst\/wiremock-standalone\/2.14.0\/wiremock-standalone-2.14.0.jar<\/pre>\n<pre>3. After the jar finished downloaded, open the terminal where the jar has been downloaded and run the following command:\njava -jar wiremock-standalone-2.14.0.jar<\/pre>\n<p>4. In the terminal you should see something like below:<\/p>\n<pre>\u279c wiremock java -jar wiremock-standalone-2.14.0.jar\nSLF4J: Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".\nSLF4J: Defaulting to no-operation (NOP) logger implementation\nSLF4J: See http:\/\/www.slf4j.org\/codes.html#StaticLoggerBinder for further details.\n \/$ \/$ \/$ \/$ \/$ \/$\n| $ \/$ | $|__\/ | $$ \/$$ | $\n| $ \/$$| $ \/$ \/$$$ \/$$$ | $$ \/$$ \/$$$ \/$$$$| $ \/$\n| $\/$ $ $| $ \/$__ $ \/$__ $| $ $\/$ $ \/$__ $ \/$_____\/| $ \/$\/\n| $$_ $$| $| $ \\__\/| $$$$| $ $$| $| $ \\ $| $ | $$$\/\n| $$\/ \\ $$| $| $ | $_____\/| $\\ $ | $| $ | $| $ | $_ $\n| $\/ \\ $| $| $ | $$$$| $ \\\/ | $| $$$\/| $$$$| $ \\ $\n|__\/ \\__\/|__\/|__\/ \\_______\/|__\/ |__\/ \\______\/ \\_______\/|__\/ \\__\/\n\nport: 8080\nenable-browser-proxying: false\nno-request-journal: false\nverbose: false<\/pre>\n<p>5. By the default the server will start on port 8080<br \/>\n6. Next we are going to create a mapping for our API using the JSON configuration.<br \/>\nFor this we are going to create a JSON file &#8220;myApi.json&#8221; with the following contents:<\/p>\n<pre>{\n \"request\": {\n \"method\": \"GET\",\n \"url\": \"\/myApi\"\n },\n \"response\": {\n \"status\": 200,\n \"body\": \"My API custom response\\n\"\n }\n }<\/pre>\n<p>7. We are going to place the file in the mappings folder that was created in the same place where the wiremock-standalone-2.14.0.jar is located.<br \/>\n8. Restart the wiremock server by running:<br \/>\njava -jar wiremock-standalone-2.14.0.jar<br \/>\n9. Now let&#8217;s browse to: http:\/\/localhost:8080\/test just to test that our API mapping has been picked up.<br \/>\nThe page should display something like the below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-229 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2018\/02\/Screen-Shot-2018-02-23-at-12.31.37.png\" alt=\"\" width=\"2162\" height=\"488\" \/><br \/>\n10. Browsing to our mapped API should respond with the configured response (status code 200 and with the body &#8220;My API custom response&#8221;)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-230 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2018\/02\/Screen-Shot-2018-02-23-at-12.31.26.png\" alt=\"\" width=\"1706\" height=\"470\" \/><!-- pn-faq --><\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>Why mock an API you already have?<\/h3>\n<p>To produce the failures the real one will not give you on demand: timeouts, 500s, malformed bodies, a slow response. You cannot ask a staging dependency to break on cue, and those are exactly the paths that cause production incidents.<\/p>\n<h3>Do I need to add WireMock to my build?<\/h3>\n<p>Not for this. The standalone jar runs as its own server from the command line, which keeps the mock out of your application&#8217;s dependencies and lets other services point at it too.<\/p>\n<h3>How do I stop mocks from drifting away from the real API?<\/h3>\n<p>Pin the stubs to the real contract and check them against it. A mock that nobody verifies eventually describes an API that no longer exists, and then the tests pass while production fails.<\/p>\n<p><!-- \/pn-faq --><!-- pn-related-reading --><\/p>\n<h2>Related reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2014\/06\/run-apache-jmeter-on-mac\">Run Apache JMeter on Mac<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2020\/09\/how-to-install-extra-plugins-in-jmeter-using-the-plugins-manager\">How to Install Plugins using the JMeter Plugin Manager<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Key takeaways WireMock lets you produce failures a real API will not give you on demand: timeouts, 500s, malformed bodies. Negative paths are where most production incidents live and where coverage is usually thinnest. Pin stubs to the real contract so a provider change breaks the test rather than production. Why do we need to&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2018\/02\/mocking-apis-using-wiremock-for-testing-negative-cases-when-it-is-not-possible-to-change-the-api-behaviour\" class=\"more-link\" title=\"Read Mocking APIs using WireMock for testing negative cases when it is not possible to change the API behaviour\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":507,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[38,39],"class_list":["post-228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-mocking-apis","tag-negative-testing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":4,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/228\/revisions\/399"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media\/507"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}