@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.
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
在应用任何测试变更之前,确保项目可以编译。如果编译失败,请立即停止。应用改进后,运行完整验证。
强制要求:在应用任何变更前运行
./mvnw compile
或
mvn compile
安全提示:如果编译失败,立即停止
验证:应用改进后运行
./mvnw clean verify
或
mvn clean verify
应用前须知:阅读参考文档以了解详细规则和优/劣模式示例
When to use this skill
何时使用本技能
Review Java code for Spring Boot unit tests
Apply best practices for Spring Boot unit tests in Java code
审核Spring Boot单元测试的Java代码
在Java代码中应用Spring Boot单元测试的最佳实践
Reference
参考资料
For detailed guidance, examples, and constraints, see references/321-frameworks-spring-boot-testing-unit-tests.md.