unit-test-wiremock-rest-api by giuseppe-trisciuoglio/developer-kit
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill unit-test-wiremock-rest-api此技能提供了使用 WireMock 对外部 REST API 集成进行单元测试的全面模式。它涵盖了桩化 HTTP 响应、验证请求、测试错误场景(4xx/5xx 响应),并确保在没有真实网络依赖的情况下进行快速、可靠的测试。
在以下情况下使用此技能:
按照以下步骤使用 WireMock 测试外部 REST API:
在测试作用域中包含 wiremock,以及 JUnit 5 和 AssertJ。
使用 @RegisterExtension 和 WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()) 进行动态端口分配。
使用 wireMock.getRuntimeInfo().getHttpBaseUrl() 将 WireMock 基础 URL 注入到您的 API 客户端中。
使用 stubFor() 来定义请求匹配和响应行为。
调用与服务交互的外部 API 的服务方法。
使用 AssertJ 断言验证服务行为。
使用 verify() 确保正确的请求被发送到外部 API。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
dependencies {
testImplementation("org.wiremock:wiremock:3.4.1")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.assertj:assertj-core")
}
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
class ExternalWeatherServiceTest {
@RegisterExtension
static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();
@Test
void shouldFetchWeatherDataFromExternalApi() {
wireMock.stubFor(get(urlEqualTo("/weather?city=London"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"city\":\"London\",\"temperature\":15,\"condition\":\"Cloudy\"}")));
String baseUrl = wireMock.getRuntimeInfo().getHttpBaseUrl();
WeatherApiClient client = new WeatherApiClient(baseUrl);
WeatherData weather = client.getWeather("London");
assertThat(weather.getCity()).isEqualTo("London");
assertThat(weather.getTemperature()).isEqualTo(15);
wireMock.verify(getRequestedFor(urlEqualTo("/weather?city=London"))
.withHeader("Accept", containing("application/json")));
}
}
@Test
void shouldHandleNotFoundError() {
wireMock.stubFor(get(urlEqualTo("/api/users/999"))
.willReturn(aResponse()
.withStatus(404)
.withBody("{\"error\":\"User not found\"}")));
WeatherApiClient client = new WeatherApiClient(wireMock.getRuntimeInfo().getHttpBaseUrl());
assertThatThrownBy(() -> client.getUser(999))
.isInstanceOf(UserNotFoundException.class)
.hasMessageContaining("User not found");
}
@Test
void shouldRetryOnServerError() {
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);
}
@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")));
}
@RegisterExtension 在测试之间自动重置 WireMock@RegisterExtension 时,WireMock 会在测试之间自动重置@Service
public class WeatherService {
private final WeatherApiClient client;
public WeatherData getWeather(String city) {
return client.fetchWeather(city);
}
}
@RegisterExtension
static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();
@Test
void shouldFetchWeatherFromExternalApi() {
wireMock.stubFor(get(urlEqualTo("/weather?city=London"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"city\":\"London\",\"temperature\":15}")));
WeatherApiClient client = new WeatherApiClient(
wireMock.getRuntimeInfo().getHttpBaseUrl()
);
WeatherData weather = client.getWeather("London");
assertThat(weather.getTemperature()).isEqualTo(15);
}
@Test
void testWithRealApi() {
WeatherData data = weatherService.getWeather("London");
// 依赖于外部 API 的可用性
}
@Test
void testWithWireMock() {
wireMock.stubFor(get(urlPathEqualTo("/weather"))
.willReturn(aResponse().withStatus(200).withBody("{}")));
// 快速、可靠的测试,具有可预测的行为
}
WireMock 未拦截请求:确保您的 HTTP 客户端使用来自 wireMock.getRuntimeInfo().getHttpBaseUrl() 的桩化 URL。
端口冲突:始终使用 wireMockConfig().dynamicPort() 让 WireMock 选择可用端口。
每周安装次数
353
代码仓库
GitHub 星标数
173
首次出现
2026年2月3日
安全审计
安装于
claude-code281
gemini-cli270
opencode270
cursor265
codex264
github-copilot250
This skill provides comprehensive patterns for unit testing external REST API integrations using WireMock. It covers stubbing HTTP responses, verifying requests, testing error scenarios (4xx/5xx responses), and ensuring fast, reliable tests without real network dependencies.
Use this skill when:
Follow these steps to test external REST APIs with WireMock:
Include wiremock in test scope along with JUnit 5 and AssertJ.
Use @RegisterExtension with WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()) for dynamic port allocation.
Inject the WireMock base URL into your API client using wireMock.getRuntimeInfo().getHttpBaseUrl().
Use stubFor() to define request matching and response behavior.
Call your service methods that interact with the external API.
Verify the service behavior using AssertJ assertions.
Use verify() to ensure correct requests were sent to the external API.
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
dependencies {
testImplementation("org.wiremock:wiremock:3.4.1")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.assertj:assertj-core")
}
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
class ExternalWeatherServiceTest {
@RegisterExtension
static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();
@Test
void shouldFetchWeatherDataFromExternalApi() {
wireMock.stubFor(get(urlEqualTo("/weather?city=London"))
.withHeader("Accept", containing("application/json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"city\":\"London\",\"temperature\":15,\"condition\":\"Cloudy\"}")));
String baseUrl = wireMock.getRuntimeInfo().getHttpBaseUrl();
WeatherApiClient client = new WeatherApiClient(baseUrl);
WeatherData weather = client.getWeather("London");
assertThat(weather.getCity()).isEqualTo("London");
assertThat(weather.getTemperature()).isEqualTo(15);
wireMock.verify(getRequestedFor(urlEqualTo("/weather?city=London"))
.withHeader("Accept", containing("application/json")));
}
}
@Test
void shouldHandleNotFoundError() {
wireMock.stubFor(get(urlEqualTo("/api/users/999"))
.willReturn(aResponse()
.withStatus(404)
.withBody("{\"error\":\"User not found\"}")));
WeatherApiClient client = new WeatherApiClient(wireMock.getRuntimeInfo().getHttpBaseUrl());
assertThatThrownBy(() -> client.getUser(999))
.isInstanceOf(UserNotFoundException.class)
.hasMessageContaining("User not found");
}
@Test
void shouldRetryOnServerError() {
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);
}
@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")));
}
@RegisterExtension@RegisterExtension@Service
public class WeatherService {
private final WeatherApiClient client;
public WeatherData getWeather(String city) {
return client.fetchWeather(city);
}
}
@RegisterExtension
static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();
@Test
void shouldFetchWeatherFromExternalApi() {
wireMock.stubFor(get(urlEqualTo("/weather?city=London"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"city\":\"London\",\"temperature\":15}")));
WeatherApiClient client = new WeatherApiClient(
wireMock.getRuntimeInfo().getHttpBaseUrl()
);
WeatherData weather = client.getWeather("London");
assertThat(weather.getTemperature()).isEqualTo(15);
}
@Test
void testWithRealApi() {
WeatherData data = weatherService.getWeather("London");
// Depends on external API availability
}
@Test
void testWithWireMock() {
wireMock.stubFor(get(urlPathEqualTo("/weather"))
.willReturn(aResponse().withStatus(200).withBody("{}")));
// Fast, reliable test with predictable behavior
}
WireMock not intercepting requests : Ensure your HTTP client uses the stubbed URL from wireMock.getRuntimeInfo().getHttpBaseUrl().
Port conflicts : Always use wireMockConfig().dynamicPort() to let WireMock choose available port.
Weekly Installs
353
Repository
GitHub Stars
173
First Seen
Feb 3, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code281
gemini-cli270
opencode270
cursor265
codex264
github-copilot250
Lark Mail CLI 使用指南:邮件管理、安全规则与自动化工作流
7,100 周安装
Rust调用关系图生成器 - 可视化函数调用层次结构,提升代码分析效率
539 周安装
parallel-web-extract:并行网页内容提取工具,高效抓取网页数据
595 周安装
腾讯云CloudBase AI模型Web技能:前端调用混元/DeepSeek模型,实现流式文本生成
560 周安装
Apollo Connectors 模式助手:GraphQL API 连接器开发与集成指南
565 周安装
GitHub Trending 趋势分析工具:实时发现热门项目、技术洞察与开源机会
556 周安装
GSAP React 集成教程:useGSAP Hook 动画库与 React 组件开发指南
546 周安装