spring-boot-testing
它能做什么
这个工具可以帮你测试 Spring Boot 代码。它会根据你想验证的内容,挑选最合适的测试方法,并使用现代工具让测试更容易编写、更清晰易懂。
适用场景
- ✓当你想测试某个网站“端点”(即通信端口)是否正常工作时。
- ✓当你想检查程序中负责保存和读取数据的部分是否正确时。
- ✓当你需要测试程序如何通过互联网与其他服务通信时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/spring-boot-testing/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何测试保存数据到数据库的代码是否正常工作?”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会分析你想测试的内容,并给出建议的方法。例如,测试界面部分可以使用 `@WebMvcTest`,测试数据部分可以使用 `@DataJpaTest`。它还建议使用 AssertJ 等库,让测试代码更易读。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Spring Boot Testing
This skill provides expert guide for testing Spring Boot 4 applications with modern patterns and best practices.
Core Principles
- Test Pyramid: Unit (fast) > Slice (focused) > Integration (complete)
- Right Tool: Use the narrowest slice that gives you confidence
- AssertJ Style: Fluent, readable assertions over verbose matchers
- Modern APIs: Prefer MockMvcTester and RestTestClient over legacy alternatives
Which Test Slice?
| 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 |
Test Slices Reference
- references/test-slices-overview.md - Decision matrix and comparison
- references/webmvctest.md - Web layer with MockMvc
- references/datajpatest.md - Data layer with Testcontainers
- references/restclienttest.md - REST client testing
Testing Tools Reference
- references/mockmvc-tester.md - AssertJ-style MockMvc (3.2+)
- references/mockmvc-classic.md - Traditional MockMvc (pre-3.2)
- references/resttestclient.md - Spring Boot 4+ REST client
- references/mockitobean.md - Mocking dependencies
Assertion Libraries
- references/assertj-basics.md - Scalars, strings, booleans, dates
- references/assertj-collections.md - Lists, Sets, Maps, arrays
Testcontainers
- references/testcontainers-jdbc.md - PostgreSQL, MySQL, etc.
Test Data Generation
- references/instancio.md - Generate complex test objects (3+ properties)
Performance & Migration
- references/context-caching.md - Speed up test suites
- references/sb4-migration.md - Spring Boot 4.0 changes
Quick Decision Tree
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
Spring Boot 4 Highlights
- RestTestClient: Modern alternative to TestRestTemplate
- @MockitoBean: Replaces @MockBean (deprecated)
- MockMvcTester: AssertJ-style assertions for web tests
- Modular starters: Technology-specific test starters
- Context pausing: Automatic pausing of cached contexts (Spring Framework 7)
Testing Best Practices
Code Complexity Assessment
When a method or class is too complex to test effectively:
- Analyze complexity - If you need more than 5-7 test cases to cover a single method, it's likely too complex
- Recommend refactoring - Suggest breaking the code into smaller, focused functions
- User decision - If the user agrees to refactor, help identify extraction points
- Proceed if needed - If the user decides to continue with the complex code, implement tests despite the difficulty
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;
}
Avoid Code Redundancy
…
常见问题
什么是“测试金字塔”?
它是一种组织测试的方式:大量小而快的单元测试,一些中等规模的切片测试,以及少量大而慢的集成测试。
为什么要使用 AssertJ?
AssertJ 使用更接近自然语言的写法来检查结果是否正确,让测试更容易阅读和编写。
什么是“测试切片”?
测试切片是只针对应用程序特定部分的测试,例如只测试网络层或只测试数据层,这样测试更快,也更有针对性。
详情
作者的原始描述
Expert Spring Boot 4 testing specialist that selects the best Spring Boot testing techniques for your situation with Junit 6 and AssertJ.
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7431
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。