spring-boot-testing by github/awesome-copilot
npx skills add https://github.com/github/awesome-copilot --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 |
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
当一个方法或类过于复杂而难以有效测试时:
重构建议示例:
// 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;
}
为常用对象和模拟设置创建辅助方法,以增强可读性和可维护性。
使用描述性的显示名称来阐明测试意图:
@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>
<!-- 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>
每周安装量
197
仓库
GitHub 星标数
26.9K
首次出现
6 天前
安全审计
已安装于
codex183
gemini-cli182
opencode182
cursor180
kimi-cli178
github-copilot178
This skill provides 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 | references/test-slices-overview.md |
| Full application | @SpringBootTest | 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
197
Repository
GitHub Stars
26.9K
First Seen
6 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex183
gemini-cli182
opencode182
cursor180
kimi-cli178
github-copilot178
Spring Boot工程师技能指南:微服务架构、安全加固与云原生开发实战
4,400 周安装