Skip to main content
Back to blog
tools 3 February 2020 3 min read

Wiremock

A guide to using Wiremock for creating mock servers during early-stage performance testing when full end-to-end environments are not available.

M

Mark

Performance Testing Expert

Performance testing early in the product development life cycle often means that a full end-to-end environment is not available. Wiremock addresses this challenge by enabling developers to rapidly create mock servers using JSON configuration files.

What is Wiremock?

Wiremock is a standalone Java application that’s freely available at wiremock.org, with optional professional support. It allows you to simulate API responses without requiring the actual backend services to be running.

Key Features

  • Lightweight and Fast - Capable of handling thousands of requests per second on standard hardware
  • Easy Configuration - Uses flat JSON files for setup
  • Built-in Templating - Includes a transformation engine for dynamic responses
  • Delay Simulation - Can replicate realistic server response times
  • Admin Interface - Includes Swagger UI for visual configuration

Implementation Steps

  1. Verify Java installation - Ensure Java is installed on your system
  2. Copy the JAR file - Download the Wiremock standalone JAR
  3. Start the server - Launch Wiremock from the command line
  4. Configure responses - Either manually create JSON files or use the admin UI
  5. Deploy JSON configurations - Place mapping files in the appropriate directory

Starting the Server

java -jar wiremock-standalone-2.27.2.jar --port 8080

Directory Structure

After starting Wiremock, it creates the following directory structure:

.
├── mappings/       # JSON stub definitions
└── __files/        # Response body files

JSON Configuration Examples

Basic Request-Response Mapping

{
  "request": {
    "method": "GET",
    "url": "/api/user/123"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{\"id\": 123, \"name\": \"Test User\"}"
  }
}

Dynamic Response with Templating

{
  "request": {
    "method": "GET",
    "urlPathPattern": "/api/user/.*"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{\"id\": \"{{request.pathSegments.[2]}}\", \"uuid\": \"{{randomValue type='UUID'}}\"}",
    "transformers": ["response-template"]
  }
}

Simulating Delays

{
  "request": {
    "method": "GET",
    "url": "/api/slow-endpoint"
  },
  "response": {
    "status": 200,
    "fixedDelayMilliseconds": 2000,
    "body": "Delayed response"
  }
}

Variable Delay Distribution

{
  "request": {
    "method": "POST",
    "url": "/api/process"
  },
  "response": {
    "status": 200,
    "delayDistribution": {
      "type": "lognormal",
      "median": 1000,
      "sigma": 0.25
    },
    "body": "{\"status\": \"processed\"}"
  }
}

Use Cases for Performance Testing

  1. Isolating components - Test your application without backend dependencies
  2. Simulating failure scenarios - Return error responses to test error handling
  3. Testing timeouts - Add delays to verify timeout configurations
  4. Load testing - Generate predictable responses at high volume

Further Reading

Tags:

#wiremock #mocking #api #performance-testing #java

Need help with performance testing?

Let's discuss how I can help improve your application's performance.

Get in Touch