Wiremock
A guide to using Wiremock for creating mock servers during early-stage performance testing when full end-to-end environments are not available.
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
- Verify Java installation - Ensure Java is installed on your system
- Copy the JAR file - Download the Wiremock standalone JAR
- Start the server - Launch Wiremock from the command line
- Configure responses - Either manually create JSON files or use the admin UI
- 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
- Isolating components - Test your application without backend dependencies
- Simulating failure scenarios - Return error responses to test error handling
- Testing timeouts - Add delays to verify timeout configurations
- Load testing - Generate predictable responses at high volume
Further Reading
Tags: