<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>WireMock on hiquality.dev</title><link>https://hiquality.dev/tags/wiremock/</link><description>Recent content in WireMock on hiquality.dev</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 13 May 2026 21:00:00 +0200</lastBuildDate><atom:link href="https://hiquality.dev/tags/wiremock/index.xml" rel="self" type="application/rss+xml"/><item><title>Getting Started with WireMock</title><link>https://hiquality.dev/posts/getting-started-with-wiremock/</link><pubDate>Wed, 13 May 2026 21:00:00 +0200</pubDate><guid>https://hiquality.dev/posts/getting-started-with-wiremock/</guid><summary>&lt;p&gt;Recently I was conducting internal training on using WireMock for testing web applications — especially the backend. Here I would like to share how to start WireMock and use it in your tests.&lt;/p&gt;
&lt;h2 id="what-is-wiremock"&gt;What is WireMock?&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://wiremock.org/"&gt;WireMock&lt;/a&gt; is a flexible and powerful API mocking tool that allows developers and testers to simulate HTTP-based APIs. It provides a simple and intuitive interface for creating mock APIs, making it easier to test applications that depend on external services. With WireMock, you can create mock responses, simulate various scenarios, and validate the behaviour of your application under different conditions.&lt;/p&gt;</summary><description>&lt;p&gt;Recently I was conducting internal training on using WireMock for testing web applications — especially the backend. Here I would like to share how to start WireMock and use it in your tests.&lt;/p&gt;
&lt;h2 id="what-is-wiremock"&gt;What is WireMock?&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://wiremock.org/"&gt;WireMock&lt;/a&gt; is a flexible and powerful API mocking tool that allows developers and testers to simulate HTTP-based APIs. It provides a simple and intuitive interface for creating mock APIs, making it easier to test applications that depend on external services. With WireMock, you can create mock responses, simulate various scenarios, and validate the behaviour of your application under different conditions.&lt;/p&gt;
&lt;h2 id="when-to-use-wiremock"&gt;When to use WireMock?&lt;/h2&gt;
&lt;p&gt;WireMock was designed to allow testing of web applications by mocking different responses from external APIs they use. It allows me to test more scenarios than would be possible using a production external API.&lt;/p&gt;
&lt;p&gt;In my daily work I usually test asynchronous APIs. In these scenarios, WireMock acts as an HTTP server where my system under test sends callbacks and notifications.&lt;/p&gt;
&lt;p&gt;WireMock helps me solve several problems encountered when testing web applications that depend on external APIs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the external API is unstable, making test results unpredictable;&lt;/li&gt;
&lt;li&gt;the API has low rate limits that are not sufficient for all required tests;&lt;/li&gt;
&lt;li&gt;the API does not exist yet and all we know is the API specification.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To be absolutely sure the application under test works correctly, I also need to perform some end-to-end testing with a real API, because mocks might not match the external API exactly.&lt;/p&gt;
&lt;h2 id="running-wiremock"&gt;Running WireMock&lt;/h2&gt;
&lt;h3 id="necessary-dependencies"&gt;Necessary dependencies&lt;/h3&gt;
&lt;p&gt;To use WireMock in tests, add the following dependency to your Maven project:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-xml"&gt;&amp;lt;dependency&amp;gt;
 &amp;lt;groupId&amp;gt;org.wiremock&amp;lt;/groupId&amp;gt;
 &amp;lt;artifactId&amp;gt;wiremock&amp;lt;/artifactId&amp;gt;
 &amp;lt;version&amp;gt;3.13.2&amp;lt;/version&amp;gt;
 &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or for SBT:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-scala"&gt;libraryDependencies += &amp;quot;org.wiremock&amp;quot; % &amp;quot;wiremock&amp;quot; % &amp;quot;3.13.2&amp;quot; % Test
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A WireMock instance can be run in several modes — managed by the test framework, started directly from code, or run in standalone mode.&lt;/p&gt;
&lt;h3 id="wiremock-instance-managed-by-the-test-framework"&gt;WireMock instance managed by the test framework&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s start with the easiest way to get WireMock running.&lt;/p&gt;
&lt;p&gt;JUnit can manage a WireMock instance. I will use the JUnit 5 WireMock extension that provides the &lt;code&gt;@WireMockTest&lt;/code&gt; annotation:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;@WireMockTest
public class HelloWireMockTest {

 @Test
 void hello_stub_test() {
 // Let's create a first stub
 stubFor(get(&amp;quot;/hello&amp;quot;).willReturn(ok()));

 // And test if the stub works using RestAssured
 given().baseUri(&amp;quot;http://localhost:8080&amp;quot;)
 .when().get(&amp;quot;/hello&amp;quot;)
 .then().assertThat().statusCode(200);
 }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For this test scenario, the test framework will start the WireMock instance and I don&amp;rsquo;t need to worry about creating one in the test or connecting to the standalone one.&lt;/p&gt;
&lt;p&gt;By default, the WireMock extension starts a WireMock instance on port 8080. You can change it by specifying the &lt;code&gt;httpPort&lt;/code&gt; parameter on the annotation, i.e.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;@WireMockTest(httpPort = 9999)
public class HelloWireMockTest {}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are still using JUnit 4, see how to do the same for that version of the test framework: &lt;a href="https://wiremock.org/docs/junit-extensions/"&gt;WireMock Docs » JUnit 4 and Vintage&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id="starting-instance-programmatically"&gt;Starting instance programmatically&lt;/h3&gt;
&lt;p&gt;If my test framework is not JUnit but is JVM-based, I can start a WireMock instance programmatically:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;public class ExamplePlainJavaTest {

 private static WireMockServer server;

 @BeforeAll
 public static void setup() {
 // Start WireMock instance
 server = new WireMockServer(options().port(9999));
 server.start();
 // Configure WireMock client to use port 9999
 WireMock.configureFor(9999);
 }

 @AfterAll
 public static void teardown() {
 server.stop();
 }

 @Test
 public void wiremock_example() {
 stubFor(get(&amp;quot;/hello&amp;quot;).willReturn(ok()));
 }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the &lt;code&gt;setup()&lt;/code&gt; method I am starting a WireMock instance on port 9999. Then I need to configure the WireMock client to the proper port to be able to use the API the same way as in the previous example.&lt;/p&gt;
&lt;p&gt;After the tests are finished, I need to remember to stop the WireMock instance in the &lt;code&gt;teardown()&lt;/code&gt; method.&lt;/p&gt;
&lt;h3 id="wiremock-standalone-instance"&gt;WireMock standalone instance&lt;/h3&gt;
&lt;p&gt;Usually for my testing I use WireMock in standalone mode with a Docker container.&lt;/p&gt;
&lt;p&gt;To start a Docker image use the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;docker run -it --rm \
 -p 8080:8080 \
 --name wiremock \
 wiremock/wiremock:3.13.2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively, you can download the JAR file using the link provided in &lt;a href="https://wiremock.org/docs/download-and-installation/#direct-download"&gt;WireMock Docs » Direct Download&lt;/a&gt; and start it with:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-bash"&gt;java -jar wiremock-standalone-3.13.2.jar
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case you can use the WireMock API directly without any prior configuration, as the default 8080 port is used.&lt;/p&gt;
&lt;p&gt;In both cases, it is worth adding the &lt;code&gt;--verbose&lt;/code&gt; option to the command to be able to inspect the requests and responses that WireMock handles during the test.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary&lt;/h2&gt;
&lt;p&gt;WireMock is a practical tool for simulating external HTTP APIs, especially when the real service is unstable, rate-limited, or not yet available. This article showed how to add WireMock as a test dependency, run it under JUnit 5, start it programmatically from JVM code, and use it as a standalone service in Docker or via the JAR.&lt;/p&gt;
&lt;p&gt;In the next articles I will show how to construct more robust stubs, test error scenarios and use more advanced features of WireMock.&lt;/p&gt;</description></item></channel></rss>