322-frameworks-spring-boot-testing-integration-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJava Integration Testing Guidelines
Java集成测试指南
Apply Java integration testing guidelines for Spring Boot 4.0.x.
What is covered in this Skill?
- Integration test scope and purpose (verify wiring and contracts, not unit-test duplication)
- Testcontainers with @ServiceConnection for zero-config wiring (preferred)
- @DynamicPropertySource as fallback for containers without built-in service connection support
- Static @Container instances shared across test methods for performance
- MockMvcTester for fluent AssertJ-based HTTP assertions (Spring Boot 4.0.x)
- TestRestTemplate for full HTTP stack testing
- @DataJdbcTest / @DataJpaTest persistence slices (load only persistence layer, start faster)
- Data isolation: each test owns its scenario; no shared mutable state or ordering assumptions
- @MockitoBean for mock registration (Spring Boot 4.0.x — @MockBean removed)
- Resource lifecycle: Testcontainers JUnit integration for teardown; *IT / integration test separation
Scope: Apply recommendations based on the reference rules and good/bad code examples.
应用适用于Spring Boot 4.0.x的Java集成测试指南。
本Skill涵盖哪些内容?
- 集成测试的范围与目的(验证组件装配与契约,而非重复单元测试的内容)
- 搭配@ServiceConnection的Testcontainers(零配置装配,推荐方案)
- 对不支持内置服务连接的容器,使用@DynamicPropertySource作为备选方案
- 跨测试方法共享静态@Container实例以提升性能
- 基于AssertJ的流畅式HTTP断言工具MockMvcTester(Spring Boot 4.0.x)
- 用于完整HTTP栈测试的TestRestTemplate
- @DataJdbcTest / @DataJpaTest持久化切片(仅加载持久化层,启动速度更快)
- 数据隔离:每个测试独立运行场景;无共享可变状态或执行顺序依赖
- @MockitoBean用于Mock注册(Spring Boot 4.0.x中已移除@MockBean)
- 资源生命周期:Testcontainers与JUnit集成以实现自动销毁;*IT / 集成测试分离
范围: 基于参考规则与代码示例(优劣对比)应用相关建议。
Constraints
约束条件
Before applying any integration test changes, ensure the project compiles. If compilation fails, stop immediately. After applying improvements, run full verification.
- MANDATORY: Run or
./mvnw compilebefore applying any changemvn compile - SAFETY: If compilation fails, stop immediately
- VERIFY: Run or
./mvnw clean verifyafter applying improvementsmvn clean verify - BEFORE APPLYING: Read the reference for detailed rules and good/bad patterns
在应用任何集成测试变更前,确保项目可编译。若编译失败,立即停止操作。在应用改进后,执行完整验证。
- 强制要求:在应用任何变更前运行或
./mvnw compilemvn compile - 安全准则:若编译失败,立即停止
- 验证步骤:在应用改进后运行或
./mvnw clean verifymvn clean verify - 应用前准备:阅读参考文档以了解详细规则与优劣模式
When to use this skill
何时使用本Skill
- Review Java code for Spring Boot integration tests
- Apply best practices for Spring Boot integration tests in Java code
- 审查Spring Boot集成测试的Java代码
- 在Java代码中应用Spring Boot集成测试的最佳实践
Reference
参考资料
For detailed guidance, examples, and constraints, see references/322-frameworks-spring-boot-testing-integration-tests.md.
如需详细指导、示例与约束条件,请查看references/322-frameworks-spring-boot-testing-integration-tests.md。