WireMock is a tool for mocking HTTP-based APIs that runs in the unit tests, on the desktop, or in the test environment. We can also say it is a simulator for HTTP-based APIs, considered a service virtualization tool or a mock server. It enables you to stay productive when an API you depend on Doesn’t exist and, Isn’t complete or, Costly to access. It supports the testing of Edge cases and failure modes. It’s fast so reduces build time significantly. In simple terms, Wiremock is a mocking setup for integration testing. It is mainly used during the development and more significantly during the Integration testing while a system or service talks to one or multiple external or internal dependencies/services.
Read more about WireMock in this article: Introduction to WireMock
In this article, we are going to use WireMock with JUnit Test. JUnit framework is a Java framework that is widely used for testing. It supports the test to run by writing and testing along. JUnit framework was initially based on the SUnit framework which is used for Unit testing but then later it was updated with Java using Selenium WebDriver. JUnit is now used as a standard when we need to perform testing in Java.
Step By Step Implementation
Step 1: Refer to the article How to Create a Maven Project in IntelliJ IDEA and create a Maven project in IntelliJ IDEA.
Step 2: Add the following dependencies in your pom.xml file.
<!-- Dependency for okhttp --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.2.2</version> </dependency> <!-- Dependency for Wiremock--> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>2.27.2</version> <scope>test</scope> </dependency> <!-- Dependency for JUnit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13-rc-2</version> <scope>test</scope> </dependency>
Below is the complete code for the pom.xml file.
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < groupId >org.example</ groupId > < artifactId >wiremock-with-junit</ artifactId > < version >1.0-SNAPSHOT</ version > < properties > < maven.compiler.source >11</ maven.compiler.source > < maven.compiler.target >11</ maven.compiler.target > </ properties > < dependencies > < dependency > < groupId >com.squareup.okhttp3</ groupId > < artifactId >okhttp</ artifactId > < version >4.2.2</ version > </ dependency > < dependency > < groupId >com.github.tomakehurst</ groupId > < artifactId >wiremock</ artifactId > < version >2.27.2</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.13-rc-2</ version > < scope >test</ scope > </ dependency > </ dependencies > </ project > |
Step 3: Now go to the src > test > java and create a test class named WiremockWithJunit. In this class, we are going to perform the JUnit test using WireMock. First, refer to the whole code then we will understand the code in detail.
Java
import com.github.tomakehurst.wiremock.junit.WireMockRule; import okhttp3.*; import org.junit.Rule; import org.junit.Test; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.junit.Assert.assertEquals; public class WiremockWithJunit { @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port( 9090 )); // WireMock with JUnit @Test public void wiremock_with_junit_test() throws Exception { // stub configuration configStub(); // call request in WireMock through OkHttpClient OkHttpClient client = new OkHttpClient().newBuilder() .build(); Request request = new Request.Builder() .method( "GET" , null ) .build(); Response response = client.newCall(request).execute(); // assert the response assertEquals( "Welcome to Lazyroar!!" , response.body().string()); } private void configStub() { configureFor( "localhost" , 9090 ); // create a stub stubFor(get(urlEqualTo( "/gfg/article1" )).willReturn(aResponse().withBody( "Welcome to Lazyroar!!" ))); } } |
Explanation: A Rule is nothing but a concept in JUnit which actually combines test setup methods like @Before and @After. And WireMock by default provides a Rule for JUnit named WireMockRule.
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(9090));
Then we define a method wiremock_with_junit_test() and annotated it with @Test annotation. Because this is the method we are going to test. And inside this method, we are going to perform the following 3 task
- Arrange
- Act
- Assert
Arrange: At first, we configure the stub in configStub() method. Refer to the below code snippet.
private void configStub() { configureFor("localhost", 9090); // create a stub stubFor(get(urlEqualTo("/gfg/article1")).willReturn(aResponse().withBody("Welcome to Lazyroar!!"))); }
Act: Then we call the request in WireMock through OkHttpClient. Refer to the below code snippet.
OkHttpClient client = new OkHttpClient().newBuilder().build(); Request request = new Request.Builder() .url("http://localhost:9090/gfg/article1") .method("GET", null) .build(); Response response = client.newCall(request).execute();
Assert: Here we will assert the response. Refer to the below code snippet.
assertEquals("Welcome to Lazyroar!!", response.body().string());
Now, let’s test the method.
Output: Success Case
And you can see Test Passed in this case. Let’s have a scenario with a Failure case.
Output: Failure Case
Now let’s change the expected value to something like this. You can also change the actual value.
assertEquals("Welcome to Hello World!!", response.body().string());
And in the output, you can see Test failed with the reason.