Stubbing External APIs in Unit Tests

Gift Hove
About Coding
Published in
3 min readSep 26, 2018

Lately, I have been working on a legacy application where i am decommissioning the back-end layer (old database of the application), to use a new and improved HTTP RESTful API. The API is still being developed by another team and I have swagger documentation to work from… which is great.

Testing my service layer(the consumer of the third part API) was an interesting part. The API is still a moving target and the team is continuously bringing it down. My solution was to write “true” unit tests( units of source code that run in isolation without any dependencies) that do not hit the actual API, tests that are completely separated from the third party API. Tests that have a simulated RESTful http API.This journey got me to work with WireMock-Net for mocking and stubbing the API.

Stubbing: What is it?

Martin Fowler’s blog post explains in detail what mocking and stubbing is and he goes in detail to show how to achieve it in unit tests. I will not go into detail to explain this except to refer you to his blog post. I am more interested with the definitions that he quotes from “Meszaros” which are:

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test.

Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

WireMock-Net

WireMock-Net is a lightweight HTTP Mocking Server for .Net, inspired by WireMock from the Java landscape.The documentation of wiremock-net defines:

WireMock.Net is a flexible library for stubbing and mocking web HTTP responses using requests matching and response templating

The idea is to use WireMock-Net to stub the third party API by spinning up a lightweight server that will give template responses or canned answers to simulate the third party API. The mock server will respond with a set of configured responses to matched requests to the third party http API.

Setting Up WireMock stub server

To use WireMock.Net you need to pull it into your project by running

Install-Package WireMock.Net -Version 1.0.4.17

In your unit tests you start by initializing the stub server in the setup method (you can start the server anyway but i prefer the setup method). The code snippet below shows how to initialize and start WireMock.Net server. Starting the server is done by calling a static method start on FluentMockSever

_stubServer = FluentMockServer.Start();

Setting up the Stub Server

An HTTP Post call to the simulated API looks like the one below. You create a request specifying the path and a wrapped body. In the wrapped body you can set up your JSON object to post to the server.

HTTP Post to the stub Server

Below is a configured response.The response with a status of 200 is returned when the relative URL exactly matches “/searches”. The body of the response will be a JSON object because we setup the header “Content-Type”, “application/json”. The JSON object tempate is set in .withbody method of the response.

Response to “/searches” call

The full unit test will look like the one below. The server in this instance is auto-generating the ports for the url.

The documentation for WireMock.Net has a few examples on how to set it up. I have enjoyed working with it.

--

--