321-frameworks-spring-boot-testing-unit-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Spring Boot Unit Testing with Mockito

使用Mockito进行Spring Boot单元测试

Apply Spring Boot unit testing guidelines with Mockito.
What is covered in this Skill?
  • Pure unit tests: @ExtendWith(MockitoExtension.class), @Mock, @InjectMocks for @Service/@Component (no Spring context)
  • Slice tests: @WebMvcTest, @MockitoBean for controllers (Spring Boot 4.0.x — @MockBean removed)
  • @JsonTest for JSON serialization with correct JSON value assertions (extractingJsonPathStringValue, etc.)
  • Parameterized tests: @ParameterizedTest with @CsvSource or @MethodSource over copy-pasted methods
  • Java records for domain objects in tests
  • @TestConfiguration, @ActiveProfiles, @Primary fixed-clock beans for deterministic test setup
  • Modern mocking API: @MockitoBean / @MockitoSpyBean; @MockBean/@SpyBean are deprecated/removed in Boot 4.0.x
Scope: Apply recommendations based on the reference rules and good/bad code examples. For integration tests use @322-frameworks-spring-boot-testing-integration-tests.
应用基于Mockito的Spring Boot单元测试指南。
本Skill涵盖哪些内容?
  • 纯单元测试:针对@Service/@Component使用@ExtendWith(MockitoExtension.class)、@Mock、@InjectMocks(无需Spring上下文)
  • 切片测试:针对控制器使用@WebMvcTest、@MockitoBean(Spring Boot 4.0.x版本中@MockBean已被移除)
  • 使用@JsonTest进行JSON序列化测试,并通过正确的JSON值断言(如extractingJsonPathStringValue等)
  • 参数化测试:使用@ParameterizedTest搭配@CsvSource或@MethodSource,替代复制粘贴的方法
  • 在测试中使用Java record作为领域对象
  • 使用@TestConfiguration、@ActiveProfiles、@Primary固定时钟Bean实现确定性测试设置
  • 现代Mocking API:@MockitoBean / @MockitoSpyBean;在Boot 4.0.x版本中@MockBean/@SpyBean已被弃用/移除
适用范围: 根据参考规则和代码优劣示例应用建议。如需集成测试,请使用@322-frameworks-spring-boot-testing-integration-tests。

Constraints

约束条件

Before applying any test changes, ensure the project compiles. If compilation fails, stop immediately. After applying improvements, run full verification.
  • MANDATORY: Run
    ./mvnw compile
    or
    mvn compile
    before applying any change
  • SAFETY: If compilation fails, stop immediately
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed rules and good/bad patterns
  • EDGE CASE: If request scope is ambiguous, stop and ask a clarifying question before applying changes
  • EDGE CASE: If required inputs, files, or tooling are missing, report what is missing and ask whether to proceed with setup guidance
在应用任何测试变更前,确保项目可编译。如果编译失败,请立即停止。应用改进后,运行完整验证。
  • 强制要求:在应用任何变更前运行
    ./mvnw compile
    mvn compile
  • 安全要求:如果编译失败,立即停止
  • 验证要求:应用改进后运行
    ./mvnw clean verify
    mvn clean verify
  • 应用前:阅读参考文档了解详细规则和优劣模式
  • 边缘情况:如果请求范围不明确,停止操作并询问澄清问题后再进行变更
  • 边缘情况:如果缺少所需输入、文件或工具,报告缺失内容并询问是否需要提供设置指导

When to use this skill

何时使用本Skill

  • Review Java code for Spring Boot unit tests
  • Apply best practices for Spring Boot unit tests in Java code
  • Write Mockito-first unit tests for Spring Boot services
  • Avoid unnecessary @SpringBootTest in Spring unit tests
  • Review Spring Boot slice-free unit test design
  • 审查Spring Boot单元测试的Java代码
  • 在Java代码中应用Spring Boot单元测试的最佳实践
  • 为Spring Boot服务编写Mockito优先的单元测试
  • 在Spring单元测试中避免不必要的@SpringBootTest
  • 审查Spring Boot无切片单元测试设计

Workflow

工作流程

  1. Read reference and assess project context
Read
references/321-frameworks-spring-boot-testing-unit-tests.md
and inspect the current project setup before proposing changes.
  1. Gather scope and decide target improvements
Identify requested outcomes, constraints, and the minimum safe set of changes to apply.
  1. Apply framework-aligned changes
Implement or refactor configuration/code following the reference patterns and project conventions.
  1. Run verification and report results
Execute appropriate build/tests and summarize what changed, what was verified, and any follow-up actions.
  1. 阅读参考文档并评估项目上下文
在提出变更前,阅读
references/321-frameworks-spring-boot-testing-unit-tests.md
并检查当前项目设置。
  1. 收集范围并确定目标改进点
明确请求的结果、约束条件,以及需要应用的最小安全变更集。
  1. 应用符合框架的变更
遵循参考模式和项目约定实现或重构配置/代码。
  1. 运行验证并报告结果
执行适当的构建/测试,总结变更内容、验证情况以及后续行动。

Reference

参考资料

For detailed guidance, examples, and constraints, see references/321-frameworks-spring-boot-testing-unit-tests.md.
如需详细指导、示例和约束条件,请查看references/321-frameworks-spring-boot-testing-unit-tests.md