Testing using Wiremock-Configuration-Part2

Harshit Shah
3 min readSep 2, 2021

--

If you guys haven’t read what is wiremock and how it is helpful for our Test activities please refer to my previous blog where I have explained what is wiremock and how can we download and use in our automation framework.

This blog will describe how we can configure the Wiremock and its other dependencies with Junit, how to start the mock server,using @rule of Junit to start before starting of our Test and how can we stop the server.

Configuration of Wiremock:

First step is to download the dependency as below.

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>

After above step is done we have to static import the wiremock server as below.

import static com.github.tomakehurst.wiremock.client.WireMock.*;

We can use use Wiremock in 2 ways either via Junit or Non-Junit. We will explore both the ways.

Junit Way:

First of all we have to do static import as described above.

Then we will use Junit @Rule annotation to start our Wiremock Server as below:

@Rule
public WireMockRule wireMockRule = new WireMockRule();

Above configuration will start the wiremock server on default port 8080.But if that port is used for some other process we can specify the other port also . Wiremock rule has many overloaded constructor which can help us in configuring this.

If we want to start on any other port apart from 8080 we can specify as below.

@Rule
public WireMockRule wireMockRule = new WireMockRule(5566);

There is one more overloaded constructor of Wiremock which take options instance to override the default setting of wiremock.

To use this we have to static import wiremockConfiguration.options class.

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

Now we can initiate the wiremock server overriding default setting as follow.

@Rule
public WireMockRule wireMockRule = new WireMockRule(options().port(5566).httpsPort(8001));

We can also configure Wiremock sever to bind with some Specific IP address as below.

@Rule
public WireMockRule wireMockRule = new WireMockRule(options().bindAddress("168.8.0.1"));

We can also ask Wiremock server to pick up the random port / HTTPS port while configuring it as below.

As you see above we have used dynamicPort()method of options to initialize the wiremock.

In our Test if we want to know on which port Wiremock server is up we can use below way to find also shown in above image. As see you in image our wiremock server has started on port 54075.

wireMockRule.start();
int port= wireMockRule.port();
System.out.println(port);

If our wiremock server requires an HTTPS connection from client and for that if we need to pass .jks certificate or trustpassword then we can use below method to specify while we are configuring our wiremock server.

@Rule
public static WireMockRule wireMockRule = new WireMockRule(options().keystorePath("pathtokeystore/jksfile.jks").
keystorePassword("xxxx").keyManagerPassword("xxx").trustStorePassword("trust"));

If our Wiremock require some proxy setting that also can be done with options() method which contain method like enableBrowserProxying(),proxyHostHeader(),proxyVia().

To stop the server we use following.

wireMockRule.stop();

Non-Junit way:

Now lets us look at the Non-Junit way of initializing the wiremock server programmatically.

We can configure wiremock server using WireMockServer class as below to run on our desired port using options() and if we don’t specify , it will start on default port i.e 8080.

WireMockServer wireMockServer = newWireMockServer(options().port(8089));

To Start the wiremock server we use :

wireMockServer.start();

To Stop the server we use.

wireMockServer.stop()

Let’s say if we are running wiremock server on some other server/container we use configureFor() method to let know the client where Wiremock is .

configureFor("127.0.0.1",8899);

That’s all folks from this blog. We will Start with mocking and in our Next Blog.

Stay tuned !!

Stay Safe ! Stay Healthy !

--

--

Harshit Shah
Harshit Shah

Written by Harshit Shah

Passionate about Testing and learning new tools and technologies.https://harshitautomation.github.io/

Responses (1)