跳到正文
Skillpedia
测试40k · #7431

spring-boot-testing

它能做什么

这个工具可以帮你测试 Spring Boot 代码。它会根据你想验证的内容,挑选最合适的测试方法,并使用现代工具让测试更容易编写、更清晰易懂。

适用场景

  • ✓当你想测试某个网站“端点”(即通信端口)是否正常工作时。
  • ✓当你想检查程序中负责保存和读取数据的部分是否正确时。
  • ✓当你需要测试程序如何通过互联网与其他服务通信时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/spring-boot-testing/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/spring-boot-testing/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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

  1. Test Pyramid: Unit (fast) > Slice (focused) > Integration (complete)
  2. Right Tool: Use the narrowest slice that gives you confidence
  3. AssertJ Style: Fluent, readable assertions over verbose matchers
  4. 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

Testing Tools Reference

Assertion Libraries

Testcontainers

Test Data Generation

Performance & Migration

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:

  1. Analyze complexity - If you need more than 5-7 test cases to cover a single method, it's likely too complex
  2. Recommend refactoring - Suggest breaking the code into smaller, focused functions
  3. User decision - If the user agrees to refactor, help identify extraction points
  4. 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能