spring-boot-testing by marcelorodrigo/agent-skills
npx skills add https://github.com/marcelorodrigo/agent-skills --skill spring-boot-testing针对 Spring Boot 4 应用程序测试的专家指南,涵盖现代模式和最佳实践。
| 场景 | 注解 | 参考文档 |
|---|---|---|
| 控制器 + HTTP 语义 | @WebMvcTest | references/webmvctest.md |
| 仓库 + JPA 查询 | @DataJpaTest | references/datajpatest.md |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| REST 客户端 + 外部 API | @RestClientTest | references/restclienttest.md |
| JSON(反)序列化 | @JsonTest | 参见 references/test-slices-overview.md |
| 完整应用程序 | @SpringBootTest | 参见 references/test-slices-overview.md |
测试控制器端点?
是 → 使用 MockMvcTester 的 @WebMvcTest
测试仓库查询?
是 → 使用 Testcontainers(真实数据库)的 @DataJpaTest
测试服务中的业务逻辑?
是 → 纯 JUnit + Mockito(无 Spring 上下文)
测试外部 API 客户端?
是 → 使用 MockRestServiceServer 的 @RestClientTest
测试 JSON 映射?
是 → @JsonTest
需要完整的集成测试?
是 → 使用最小上下文配置的 @SpringBootTest
当一个方法或类过于复杂而难以有效测试时:
重构建议示例:
// 之前:难以测试的复杂方法
public Order processOrder(OrderRequest request) {
// 验证、折扣计算、支付、库存、通知...
// 50+ 行混合关注点的代码
}
// 之后:重构为可测试的单元
public Order processOrder(OrderRequest request) {
validateOrder(request);
var order = createOrder(request);
applyDiscount(order);
processPayment(order);
updateInventory(order);
sendNotification(order);
return order;
}
为常用对象和模拟设置创建辅助方法,以增强可读性和可维护性。
使用描述性的显示名称来阐明测试意图:
@Test
@DisplayName("应为 VIP 客户计算折扣")
void shouldCalculateDiscountForVip() { }
@Test
@DisplayName("当客户信用不足时应拒绝订单")
void shouldRejectOrderForInsufficientCredit() { }
始终按此顺序组织测试:
编写测试时要考虑真实的生产场景。这使测试更具相关性,并有助于理解代码在实际生产情况下的行为。
以 80% 的代码覆盖率作为质量和努力之间的实际平衡点。更高的覆盖率是有益的,但不是唯一目标。
使用 Jacoco Maven 插件进行覆盖率报告和跟踪。
覆盖率规则:
优先考虑什么:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 用于 WebMvc 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 用于 Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
每周安装量
101
代码仓库
GitHub 星标数
6
首次出现
2026 年 1 月 31 日
安全审计
安装于
opencode91
github-copilot83
gemini-cli72
codex71
amp63
kimi-cli63
Expert guide for testing Spring Boot 4 applications with modern patterns and best practices.
| Scenario | Annotation | Reference |
|---|---|---|
| Controller + HTTP semantics | @WebMvcTest | references/webmvctest.md |
| Repository + JPA queries | @DataJpaTest | references/datajpatest.md |
| REST client + external APIs | @RestClientTest | references/restclienttest.md |
| JSON (de)serialization | @JsonTest | See references/test-slices-overview.md |
| Full application | @SpringBootTest | See references/test-slices-overview.md |
Testing a controller endpoint?
Yes → @WebMvcTest with MockMvcTester
Testing repository queries?
Yes → @DataJpaTest with Testcontainers (real DB)
Testing business logic in service?
Yes → Plain JUnit + Mockito (no Spring context)
Testing external API client?
Yes → @RestClientTest with MockRestServiceServer
Testing JSON mapping?
Yes → @JsonTest
Need full integration test?
Yes → @SpringBootTest with minimal context config
When a method or class is too complex to test effectively:
Example of refactoring recommendation:
// Before: Complex method hard to test
public Order processOrder(OrderRequest request) {
// Validation, discount calculation, payment, inventory, notification...
// 50+ lines of mixed concerns
}
// After: Refactored into testable units
public Order processOrder(OrderRequest request) {
validateOrder(request);
var order = createOrder(request);
applyDiscount(order);
processPayment(order);
updateInventory(order);
sendNotification(order);
return order;
}
Create helper methods for commonly used objects and mock setup to enhance readability and maintainability.
Use descriptive display names to clarify test intent:
@Test
@DisplayName("Should calculate discount for VIP customer")
void shouldCalculateDiscountForVip() { }
@Test
@DisplayName("Should reject order when customer has insufficient credit")
void shouldRejectOrderForInsufficientCredit() { }
Always structure tests in this order:
Write tests with real production scenarios in mind. This makes tests more relatable and helps understand code behavior in actual production cases.
Aim for 80% code coverage as a practical balance between quality and effort. Higher coverage is beneficial but not the only goal.
Use Jacoco maven plugin for coverage reporting and tracking.
Coverage Rules:
What to Prioritize:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For WebMvc tests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
Weekly Installs
101
Repository
GitHub Stars
6
First Seen
Jan 31, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode91
github-copilot83
gemini-cli72
codex71
amp63
kimi-cli63
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
116,600 周安装
Go测试最佳实践指南:编写清晰可维护的Go测试代码规范
363 周安装
Neon与Vercel无服务器PostgreSQL集成指南:快速部署、边缘兼容与数据库管理
363 周安装
Firebase Authentication 集成指南 - 快速实现用户认证与安全管理
363 周安装
TypeScript高级类型完全指南:泛型、条件类型、映射类型、实用工具类型详解
372 周安装
Claude技能:自动化文档工作流 - 智能管理项目文档生命周期(CLAUDE.md/README.md)
364 周安装
Claude项目工作流技能:9个命令自动化项目生命周期,节省35-55分钟
365 周安装