During microservice development, contract testing, and front-end integration testing, applications frequently rely on external REST API endpoints, payment gateways, or OAuth identity providers. Running integration test suites against live third-party cloud environments can be slow, costly, and unreliable due to rate limits or intermittent network failures. WireMock provides a flexible HTTP mock server that stubs responses and simulates faults. By mounting WireMock into Windows Sandbox via a .wsb XML file, developers can spin up an ephemeral API mocking server in seconds without modifying host Java runtimes. This tutorial covers the complete configuration.
The Architecture of Ephemeral Mock Testing
Windows Sandbox provides an isolated Hyper-V guest environment that communicates with the host over virtual NAT. By mapping a host directory containing the standalone WireMock JAR binary, test JSON stub mappings, and an automated PowerShell provisioning script via <MappedFolders>, the sandbox boots, installs a portable Java runtime silently, and binds WireMock to port 8080 immediately upon launch.
Structuring Host Folders and Assets
Create a dedicated automation directory on your Windows 11 host (for example, C:\SandboxWireMock):
C:\SandboxWireMock\
├── wiremock-standalone.jar
├── mappings\
│ └── test-api.json
├── Start-WireMock.ps1
└── WireMock_Sandbox.wsb
Authoring the Test Stub Mapping
Save a sample mock API definition as C:\SandboxWireMock\mappings\test-api.json:
{
"request": {
"method": "GET",
"url": "/api/v1/customer/100"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": {
"customerId": 100,
"status": "Active",
"tier": "Enterprise",
"currency": "GBP"
}
}
}
Authoring the WireMock Provisioning Script
Save the following PowerShell script as C:\SandboxWireMock\Start-WireMock.ps1. This script launches the mock server and opens a test browser window:
# Start-WireMock.ps1 - Automated WireMock Launcher
param (
[string]$JarPath = "C:\SandboxAssets\wiremock-standalone.jar",
[string]$RootDir = "C:\SandboxAssets"
)
Write-Host "Initialising WireMock API Mock Server inside Windows Sandbox..." -ForegroundColor Cyan
# 1. Check for Java runtime or download portable OpenJDK
if (-not (Get-Command "java" -ErrorAction SilentlyContinue)) {
Write-Host "Installing OpenJDK runtime via winget..." -ForegroundColor Yellow
winget install --id Microsoft.OpenJDK.17 --exact --silent --accept-package-agreements --accept-source-agreements
$env:PATH = "C:\Program Files\Microsoft\jdk-17.0.12.7-hotspot\bin;" + $env:PATH
}
# 2. Launch WireMock standalone
$WireMockProcess = Start-Process "java" -ArgumentList "-jar", $JarPath, "--port", "8080", "--root-dir", $RootDir, "--verbose" -PassThru -NoNewWindow
Write-Host "WireMock listening at http://localhost:8080" -ForegroundColor Green
# 3. Test endpoint via Edge
Start-Sleep -Seconds 3
Start-Process "msedge.exe" "http://localhost:8080/api/v1/customer/100"
# Keep session active
Wait-Process -Id $WireMockProcess.Id
Creating the WSB XML Configuration
Create WireMock_Sandbox.wsb with mapped folder access and automated script execution:
<Configuration>
<VGpu>Enable</VGpu>
<Networking>Default</Networking>
<MappedFolders>
<MappedFolder>
<HostFolder>C:\SandboxWireMock</HostFolder>
<SandboxFolder>C:\SandboxAssets</SandboxFolder>
<ReadOnly>true</ReadOnly>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>powershell.exe -ExecutionPolicy Bypass -NoExit -File C:\SandboxAssets\Start-WireMock.ps1</Command>
</LogonCommand>
</Configuration>
Executing and Auditing Mock API Services
- Double-click
WireMock_Sandbox.wsbon your Windows 11 host. - Windows Sandbox spins up in seconds, mounts the host directory, and executes PowerShell.
- WireMock starts on port 8080 and loads all JSON mappings from the
mappings/folder. - Microsoft Edge launches automatically, displaying the JSON response:
{"customerId":100,"status":"Active",...}. - Execute test suites against the sandbox endpoint. When complete, closing the sandbox terminates all servers, leaving the host pristine.