Using WireMock in API-driven development

Zaheer Moola
Nov 7 · 5 min read
Photo by Jiroe on Unsplash

I’ve recently had the misfortune of working on a project with an unstable backend. If you’re a fellow frontend developer, you likely understand the frustration caused by waiting to test something you just wrote and being completely unable to. However, the project made great use of a tool named Wiremock which made this all quite bearable.

I decided to figure out just how to do this on my own for future projects and hence, in this article, I’m going to discuss how to set up a standalone WireMock server and use it in an iOS application to set up a ‘mock data’ environment. We’ll try and make the application clever enough so that using Wiremock is a breeze. Although this tutorial will be iOS-specific, it can be performed in Android without too much difficulty.

Before I go any further, for those who haven’t heard of it before, let’s briefly talk about what WireMock is: created in 2011 by Tom Akehurst, WireMock is a mock HTTP server. It is primarily used by developers to stub or verify network requests. You can read more about it in detail here.

For this tutorial, we’re going to be looking at using Wiremock to stub network data so that we can test our application without actually using the backend. I have written a small mock project that uses the Star Wars API to display some basic information.

Above, you’ll find my incredibly simple app (do excuse the bland UI; I used this app to experiment with SwiftUI ). There are two API calls occurring here. One, to get a list of characters with details, and a second to get the details of their specific planet. I’ll add a bit more functionality for our final product, but for now, this is plenty for us to work with.

Let’s break down what we need to do now to be able to essentially run this app and maintain the same behaviour without network. We need:

  • A way to manipulate the environment we which to use when the app starts
  • A class to generate different URL hostnames based on the environment the app is running in
  • To setup Wiremock and generate the actual mock files that the app will use

Manipulating the Environment

This step will vary greatly depending on your use case. Essentially, we want a way to tell the application which environment to use before it makes any network requests. A login screen works best in this situation, but you could find a few other creative solutions. I’ll be adding a dashboard screen to my application with a simple toggle to switch to the mock data environment. Again, you can be as creative as you like with hidden gestures, specific usernames which are meant for mock environments, etc.

First, we need to create a class that will manage the environment. I’ve created a singleton class so that we maintain the environment state.

I designed a very simple view here. Toggling the mock Environment will call the setEnvironment function. This won’t do anything just yet but we’ll soon fix that.

Generating different URLs

The API that I’m using often returns URLs that contain the hostname and so, I have decided to create a function that will swap the hostnames to localhost when I’m running it in Wiremock version. I can simply call the generateURL function with a string URL and get the correct URL without providing any environment information.

Setting up Wiremock

First things first, you’re going to need to download the standalone JAR file for Wiremock from here. Let’s create a new folder in our project to store all our Wiremock files. Copy the JAR file there. Next, ‘cd’ to the folder in terminal and run: java -jar wiremock-standalone-2.25.1.jar

This should create “files” and “mappings” folders for you and start running the standalone server. That’s pretty much it. I’d recommend reading the documentation to better understand how to make full use of it. There’s a ton of improvements you could do here like creating scripts to start and stop your server but we’ll focus stop at the basics for now.

Generating Mock Files

Now comes the simple bit. Generating our mocks. Ensure your standalone server is running and then access the recording interface. You should see the screen below. Enter the hostname you wish to record and tap Record.

Let’s quickly discuss what needs to happen now. Wiremock will essentially act as a middle-man between us and the service. We can make requests to localhost and it will return responses as if we made it directly to the service, all the while recording the responses. You could use the command line to make your requests but I find Postman works wonders for this. Simply fire it up and replace your hostname with http://localhost:8080 and make a few requests. When you’re satisfied, go back to your interface and hit stop.

You should see something like this, and the mappings folder in your Wiremock folder should have a couple of JSON files added to them. Take some time to go through those files, they’re pretty simple enough that you could create them without an existing service which is great for developing something when the service isn’t ready just yet. Let’s test it now. I’ve switched off my Wi-Fi and recorded the following:

Voila! Notice how we can get responses without an internet connection if we use mocks. Also, notice my cool automatic dark mode thanks to SwiftUI. We can also manually edit our responses which can prove extremely useful.

I add a character to the Star Wars universe

The possibilities go on, I’ve just touched the tip of the iceberg here. I do hope you benefited even slightly from this and are ready to introduce Wiremock into your next project.

If you enjoyed this, do let me know and I’ll gladly make a part two where I focus on some advanced tips/techniques and how we can use Wiremock in actual business projects. Thanks for reading!

DVT Software Engineering

Making an impact in Software Engineering

Zaheer Moola

Written by

Part-time Degenerate. Full-time iOS developer :)

DVT Software Engineering

Making an impact in Software Engineering

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade