Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaIntroduction to WireMock

Introduction to WireMock

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 or
  • 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. Now let’s understand the whole WireMock concept through a simple real-world example. 

Example

For example, we are building a Muzix application and we have a feature inside this app where we have to connect with an external API provided by Spotify, or Ganna. But the question is how to perform the integration testing with the external APIs. So there will be two approaches to performing this task.

1. First Approach

This approach is so obvious. That means we can test with some testing environment or the actual production environment. But in this approach we have to face the following challenges:

  • Many times there are costs involved in hitting an API.
  • The external API system might not be always available which means we are totally dependent on the external system and any downtime in that system, will affect our tests and indirectly the release process.
  • The external API system might or might not have a test environment which means a music search API might always require a real ISRC (International Standard Recording Code) ID to fetch and return responses. 
Production System - WireMock

 

2. Second Approach 

The second approach will be why don’t you use a mock server (Wiremock) which mocks responses to the requests received for the dependency. 

Test System - WireMock

 

Features of WireMock

1. Stubbing: It is a technique that allows configuring the HTTP response that is returned by the WireMock server when it receives a specific HTTP request. You can stub HTTP requests with WireMock by using the static givenThat() method of the WireMock class.

2. Verification: The WireMock server registers all requests it receives in memory until it is reset. And that makes it possible to verify that a request matching a specific pattern was received, and also to fetch the requests’ details. 

3. Record-playback of interactions: WireMock can create stub mappings from requests it has received. Combined with its proxying feature this allows you to “record” stub mappings from interaction with existing APIs.

4. Injection of faults and delays: One of the main reasons it’s beneficial to use web service fakes when testing is to inject faulty behavior that might be difficult to get the real service to produce on-demand. 

5. Simulation of Stateful Behaviour: Most web services tend to have some state, which changes as you and others interact with them. So it’s pretty useful to be able to simulate this when you’ve swapped a real service for a test double.

6. Can be used as

  • JVM library in unit testing 
  • Run as a standalone process either on the same host or remote server or on the cloud. 

7. All of WireMock’s features are easily accessible via its REST (JSON) interface and its’ Java API. 

Maven Dependency for WireMock

In order to use the WireMock library, you need to include the following dependency in the pom.xml file

For Java 8: 

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

For Java 8 standalone:

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

Note: The Java version must be 8 or greater in order to use this dependency. 

Gradle Dependency for WireMock

In order to use the WireMock library, you need to include the following dependency in the build.gradle file. 

For Java 8: 

testImplementation "com.github.tomakehurst:wiremock-jre8:2.33.2"

For Java 8 standalone:

testImplementation "com.github.tomakehurst:wiremock-jre8-standalone:2.33.2"

Note: The Java version must be 8 or greater in order to use this dependency. 

RELATED ARTICLES

Most Popular

Recent Comments