giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-java/skills/unit-test-wiremock-rest-api
unit-test-wiremock-rest-api
VerifiedProvides patterns for unit testing external REST APIs using WireMock. Stubs API responses, verifies request details, simulates failures (timeouts, 4xx/5xx errors), and validates HTTP client behavior without real network calls. Use when testing service integrations with external APIs or mocking HTTP endpoints.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.3 KB |
| references/advanced-examples.md | markdown | 3.1 KB |
references/advanced-examples.md
View on GitHub# WireMock Advanced Examples
Additional patterns for complex WireMock testing scenarios.
## Error Scenario Testing (4xx/5xx)
```java
@Test
void shouldHandleNotFoundError() {
wireMock.stubFor(get(urlEqualTo("/api/users/999"))
.willReturn(aResponse()
.withStatus(404)
.withBody("{\"error\":\"User not found\"}")));
ApiClient client = new ApiClient(wireMock.getRuntimeInfo().getHttpBaseUrl());
assertThatThrownBy(() -> client.getUser(999))
.isInstanceOf(UserNotFoundException.class)
.hasMessageContaining("User not found");
}
@Test
void shouldHandleServerError() {
wireMock.stubFor(get(urlEqualTo("/api/data"))
.willReturn(aResponse()
.withStatus(500)
.withBody("{\"error\":\"Internal server error\"}")));
ApiClient client = new ApiClient(wireMock.getRuntimeInfo().getHttpBaseUrl());
assertThatThrownBy(() -> client.fetchData())
.isInstanceOf(ServerErrorException.class);
}
```
## Request Body Verification
```java
@Test
void shouldVerifyRequestBody() {
wireMock.stubFor(post(urlEqualTo("/api/users"))
.willReturn(aResponse()
.withStatus(201)
.withBody("{\"id\":123,\"name\":\"Alice\"}")));
ApiClient client = new ApiClient(wireMock.getRuntimeInfo().getHttpBaseUrl());
UserResponse response = client.createUser("Alice");
assertThat(response.getId()).isEqualTo(123);
wireMock.verify(postRequestedFor(urlEqualTo("/api/users"))
.withRequestBody(matchingJsonPath("$.name", equalTo("Alice")))
.withHeader("Content-Type", containing("application/json")));
}
```
## Timeout Simulation
```java
@Test
void shouldHandleTimeout() {
wireMock.stubFor(get(urlEqualTo("/api/slow"))
.willReturn(aResponse()
.withFixedDelay(5000)
.withStatus(200)));
// Configure client timeout < 5000ms
assertThatThrownBy(() -> client.fetchSlowEndpoint())
.isInstanceOf(SocketTimeoutException.class);
}
```
## Partial Response Matching
```java
@Test
void shouldMatchPartialRequestBody() {
wireMock.stubFor(post(urlEqualTo("/api/orders"))
.withRequestBody(matchingJsonPath("$.items[*]", equalTo("[1,2,3]")))
.willReturn(aResponse()
.withStatus(201)));
ApiClient client = new ApiClient(wireMock.getRuntimeInfo().getHttpBaseUrl());
client.createOrder(List.of(1, 2, 3));
wireMock.verify(postRequestedFor(urlEqualTo("/api/orders")));
}
```
## Scenarios (Stateful Behavior)
```java
@Test
void shouldSupportStatefulScenarios() {
wireMock.stubFor(get(urlEqualTo("/api/status"))
.inScenario("OrderWorkflow")
.whenScenarioStateIs(STARTED)
.willSetStateTo("PROCESSING")
.willReturn(aResponse()
.withStatus(202)
.withBody("{\"status\":\"processing\"}")));
wireMock.stubFor(get(urlEqualTo("/api/status"))
.inScenario("OrderWorkflow")
.whenScenarioStateIs("PROCESSING")
.willSetStateTo("COMPLETED")
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"status\":\"completed\"}")));
// First call
client.getOrderStatus();
wireMock.verify(getRequestedFor(urlEqualTo("/api/status"))
.inScenario("OrderWorkflow"));
// Second call
client.getOrderStatus();
wireMock.verify(getRequestedFor(urlEqualTo("/api/status")));
}
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |