132-java-testing-integration-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJava Integration testing guidelines
Java集成测试指南
Set up robust integration-test infrastructure for Java services using WireMock to stub outbound HTTP dependencies.
What is covered in this Skill?
- Infrastructure topology detection: scanning imports for ,
HttpClient,feign.*,retrofit2.*RestTemplate - Abstract base class
BaseIntegrationTest - with
WireMockExtension, dynamic port allocation (@RegisterExtension)dynamicPort() - ,
usingFilesUnderClasspath("wiremock")+@BeforeAllfor coordinate propagationSystem.setProperty() - WireMock JSON mapping files (referencing
bodyFileName)wiremock/files/ - Programmatic stub registration via WireMock DSL
- Per-test stub isolation: register stubs inside each test method
- Fault injection: 503 service unavailable, network latency with
withFixedDelay - Request verification via
WIREMOCK.verify - Maven dependency (test scope)
wiremock-standalone - Anti-patterns: global stubs, Mockito-mocked HTTP clients, hardcoded ports or URLs
@BeforeAll
Scope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
使用WireMock为Java服务搭建健壮的集成测试基础设施,以桩化出站HTTP依赖。
本技能涵盖哪些内容?
- 基础设施拓扑检测:扫描、
HttpClient、feign.*、retrofit2.*的导入信息RestTemplate - 抽象的基类
BaseIntegrationTest - 搭配的
@RegisterExtension,动态端口分配(WireMockExtension)dynamicPort() - ,通过
usingFilesUnderClasspath("wiremock")+@BeforeAll传递坐标System.setProperty() - WireMock JSON映射文件(引用
bodyFileName)wiremock/files/ - 通过WireMock DSL以编程方式注册桩
- 测试级桩隔离:在每个测试方法内注册桩
- 故障注入:503服务不可用、使用模拟网络延迟
withFixedDelay - 通过验证请求
WIREMOCK.verify - Maven依赖(测试作用域)
wiremock-standalone - 反模式:全局桩、Mockito模拟的HTTP客户端、硬编码端口或URL
@BeforeAll
适用范围:参考文档按每个核心领域的示例(优/劣代码模式)组织。根据适用的示例应用建议。
Constraints
约束条件
Before applying any integration test changes, ensure the project compiles. If compilation fails, stop immediately — do not proceed until resolved. After applying improvements, run full verification.
- MANDATORY: Run or
./mvnw compilebefore applying any changemvn compile - SAFETY: If compilation fails, stop immediately and do not proceed — compilation failure is a blocking condition
- VERIFY: Run or
./mvnw clean verifyafter applying improvementsmvn clean verify - BEFORE APPLYING: Read the reference for detailed examples, good/bad patterns, and constraints
在对集成测试进行任何修改之前,确保项目可以编译。如果编译失败,请立即停止——问题解决前不要继续。在应用改进后,运行完整验证。
- 强制要求:在进行任何修改前运行或
./mvnw compilemvn compile - 安全准则:如果编译失败,立即停止并不要继续——编译失败是阻塞性条件
- 验证步骤:应用改进后运行或
./mvnw clean verifymvn clean verify - 应用前须知:阅读参考文档以获取详细示例、优/劣模式及约束条件
When to use this skill
何时使用本技能
- Review Java code for integration tests
- Apply best practices for integration tests in Java code
- 审查Java集成测试代码
- 在Java代码中应用集成测试最佳实践
Reference
参考资料
For detailed guidance, examples, and constraints, see references/132-java-testing-integration-testing.md.
如需详细指南、示例和约束条件,请查看references/132-java-testing-integration-testing.md。