top of page
Writer's pictureAmit Ghorpade

Wiremock Wizardry: Master the Art of API Mocking!

Are you looking to mock your API? or looking to get Third party API mock? Dependent on different teams to get your API integration testing? Running behind timeline due to third party API not developed. Couldn't test API error/delay/exception scenario.


Wiremock is a solution for all these situations. Be Master of API Mocking using Wiremock and stay ahead of time.


What is WireMock?

An open source ,powerful API mocking tool.Whether you're working on a micro services architecture, building a web application, or developing a mobile app, WireMock can help you to create a virtual version of your API for testing and development purposes.


Why we need WireMock?

Dependency on third party API or to stimulate API request/response for different scenario,test API unexpected behavior response then WireMock is perfect and free to use solution on local as well on cloud.


When we are developing an API and it integrated with third party API then it is know fact that we hold/pause our development because third party API is not ready to be consumed. No more this dependency now. You can stimulate and mock third party API within minutes and build your API and test it against this Mock API with all possible API request/response scenarios.


Features/Benefits

  • HTTP response stubbing, matchable on URL, header and body content patterns

  • Request verification

  • Runs in unit tests, as a standalone process or as a WAR app

  • Record/playback of stubs

  • Configurable response delays and fault injection

  • Per-request conditional proxying

  • Browser proxying for request inspection and replacement

  • Stateful behavior simulation

WireMock Cloud solution is very easy to use but in this blog we will see how to use Wiremock Java Library to create Mock API's on local machine or you can install this in your on prem server or cloud within your organization.

WireMock Cloud solution may not be allowed in your organization due to security reason as well it comes with Licence which may cost. So lets try WireMock Library(jar).


How to Install?

Wiremock can be wadded as a dependency on your java project and used on JUnit. In this blog we will run Wiremock as a standalone service so we can create no of API stub or Mock API's.


Pre-requisite:

Please download and install AdoptOpenJDK 11 which is default openJDK from below link- https://adoptium.net/en-GB/temurin/releases/?version=11

Validate AdoptOpenJDK is installed using below and result should as below

C:\Users\abc>java -version
openjdk version "11.0.21" 2023-10-17
OpenJDK Runtime Environment Temurin-11.0.21+9 (build 11.0.21+9)
OpenJDK Client VM Temurin-11.0.21+9 (build 11.0.21+9, mixed mode)

WireMock as a Standalone Server

WireMock can run as a standalone service, configured via the Java API, JSON over HTTP or JSON files.

You can download standalone jar file from below location -

Once you download jar, please go to jar file location and execute below command to run standalone Wiremock server.

$ java -jar wiremock-standalone-3.2.0.jar
C:\Users\magar\Downloads>java -jar wiremock-standalone-3.2.0.jar

You can see WireMock server is started and it is running on port 8080. You can change port and other settings when your are going to run wiremock server. Follow below link to see options/parameters you can pass to change those settings

e.g.

--port: Set the HTTP port number e.g. --port 9999. Use --port 0 to dynamically determine a port.

--disable-http: Disable the HTTP listener, option available only if HTTPS is enabled.


Here in this post I will walk you through simplest way to mock your api using JSON file.Once you Wiremock standalone server is up and running.

In same directory now you will see two new directories are creating once server is up n running as below

__files & mappings


create stub files under mappings directory as below - hellowworldStub.json

{
  "request": {
    "method": "GET",
    "url": "/api/mytest"
  },
  "response": {
    "status": 200,
    "body": "Hello World !!\n"
  }
}

Restart your wiremock server.


make sure your file extension is .json and not anything else like .txt or something else.

If it is not a .json file then you will receive below response

No response could be served as there are no stub mappings in this WireMock instance.

Once you json file with stub is present on mappings directory. Then execute below curl command -

$ curl http://localhost:8080/api/mytest
Hello World !!

Or you can go to browser and call below URL

Best Thing about Wiremock is if you missed something in your request then it provides you nearest/closet matching url you should try in response as below

If I pass url as /api/mytests instead of /api/mytest then response will be as below



Multi-stub JSON files

You can add multipl stubs inside same json file and no need to create multiple stub json files. These are of the form:

{
  "mappings": [
    {
      "request": {
        "method": "GET",
        "url": "/one"
      },
      "response": {
        "status": 200,
        "body": "Welcome to Wiremock Stubbing!!"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/two"
      },
      "response": {
        "body": "Updated"
      }
    }
  ]
}

Pretty straightforward and easy to create stubb and you can TEST your api against this.

Now,lets consider below scenario's

API response with delay

API response with exception/error status

These are difficult to test when downstream system API cannot produce or support such scneario in integration testing. It is important for you to validate your in such scenario's then Wiremock will surely help you.


Simulating Fault Scenario

Case 1- Delay Response In case if you want to get response with certain delay then use below stub file

{
  "request": {
    "method": "GET",
    "url": "/delayed"
  },
  "response": {
    "status": 200,
    "fixedDelayMilliseconds": 2000
  }
}

If you do $curl http://localhost:8080/delayed you will response on screen after 2sec.


You can set this delay on all stub as well instead on each n every stub if you have such requirement.


Case 2- Exception/Error Response

In case if you want to get exception/error response then use below stub file


{
  "request": {
    "method": "GET",
    "url": "/fault"
  },
  "response": {
    "status": 500
    "fault": "MALFORMED_RESPONSE_CHUNK"
  }
}

I hope you have enjoyed learning about wiremock and understood how it can help you in your API testing.There are no of scenario's and possibilities you can try with wiremock to make your API's life easy.


References-


Happy Coding!!!





Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page