首页/测试 & QA/spring-boot-testing
S

spring-boot-testing

by @githubv1.0.0
4.7(3)

提供 Spring Boot 应用测试指南,涵盖单元测试、集成测试和测试切片的最佳实践

testingspring-bootjavaintegration-testingunit-testingGitHub
安装方式
npx skills add github/awesome-copilot --skill spring-boot-testing
compare_arrows

Before / After 效果对比

1
使用前

手动完成提供 Spring Boot 相关任务,需要反复查阅文档和调试,整个过程大约需要97分钟

使用后

使用该 Skill 自动化处理,8分钟内完成,流程标准化且准确率高

description SKILL.md

spring-boot-testing

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

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:

  • 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

Create helper methods for commonly used objects and mock setup to enhance readability and maintainability.

Test Organization with @DisplayName

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() { }

Test Coverage Order

Always structure tests in this order:

  • Main scenario - The happy path, most common use case

  • Other paths - Alternative valid scenarios, edge cases

  • Exceptions/Errors - Invalid inputs, error conditions, failure modes

Test Production Scenarios

Write tests with real production scenarios in mind. This makes tests more relatable and helps understand code behavior in actual production cases.

Test Coverage Goals

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:

  • 80+% coverage minimum

  • Focus on meaningful assertions, not just execution

What to Prioritize:

  • Business-critical paths (payment processing, order validation)

  • Complex algorithms (pricing, discount calculations)

  • Error handling (exceptions, edge cases)

  • Integration points (external APIs, databases)

Dependencies (Spring Boot 4)

<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 Installs297Repositorygithub/awesome-copilotGitHub Stars27.4KFirst Seen9 days agoSecurity AuditsGen Agent Trust HubPassSocketPassSnykPassInstalled ongemini-cli273opencode272codex272cursor269github-copilot268kimi-cli267

forum用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价,来写第一条吧

统计数据

安装量200
评分4.7 / 5.0
版本1.0.0
更新日期2026年3月29日
对比案例1 组

用户评分

4.7(3)
5
0%
4
0%
3
0%
2
0%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code

时间线

创建2026年3月29日
最后更新2026年3月29日