323-frameworks-spring-boot-testing-acceptance-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Spring Boot acceptance tests from Gherkin

基于Gherkin的Spring Boot验收测试

Implement acceptance tests from Gherkin feature files in Spring Boot projects. Given a .feature file in context, find @acceptance-tagged scenarios and implement happy-path tests with @SpringBootTest, TestRestTemplate, Testcontainers, and WireMock.
What is covered in this Skill?
  • Parse Gherkin .feature files to find scenarios tagged @acceptance or @acceptance-tests
  • Implement happy-path acceptance tests (one test per scenario)
  • @SpringBootTest(webEnvironment = RANDOM_PORT), @Autowired TestRestTemplate (auto-configured, no extra dependency)
  • @ServiceConnection for Testcontainers (Spring Boot 4.0.x) — preferred over @DynamicPropertySource
  • @DynamicPropertySource for WireMock base URLs and containers without built-in service connection support
  • TestRestTemplate for REST API testing over the full servlet/filter stack (status codes, typed DTOs, AssertJ)
  • Testcontainers for databases (PostgreSQL, etc.) and Kafka
  • WireMock for stubbing external REST APIs (not internal @Service beans)
  • @DisplayName echoing Gherkin scenario title for BDD fidelity
  • Given-When-Then structure mapping Gherkin steps to setup, HTTP call, and assertions
Preconditions: (1) The Gherkin .feature file must be in context. (2) The project must use Spring Boot. For framework-agnostic Java, use @133-java-testing-acceptance-tests.
Scope: Implements only happy-path scenarios. Use the reference for detailed examples and constraints.
在Spring Boot项目中从Gherkin feature文件实现验收测试。当上下文环境中存在.feature文件时,查找标记为@acceptance的场景,并使用@SpringBootTest、TestRestTemplate、Testcontainers和WireMock实现正常路径测试。
本Skill涵盖内容:
  • 解析Gherkin .feature文件,查找标记为@acceptance或@acceptance-tests的场景
  • 实现正常路径验收测试(每个场景对应一个测试用例)
  • 使用@SpringBootTest(webEnvironment = RANDOM_PORT)、@Autowired TestRestTemplate(自动配置,无需额外依赖)
  • 为Testcontainers使用@ServiceConnection(Spring Boot 4.0.x)——优先于@DynamicPropertySource
  • 为WireMock基础URL和不支持内置服务连接的容器使用@DynamicPropertySource
  • 使用TestRestTemplate在完整的servlet/filter栈上进行REST API测试(状态码、类型化DTO、AssertJ)
  • 使用Testcontainers测试数据库(如PostgreSQL等)和Kafka
  • 使用WireMock桩化外部REST API(而非内部@Service bean)
  • 使用@DisplayName呼应Gherkin场景标题,保证BDD一致性
  • 将Given-When-Then结构映射为Gherkin步骤的初始化、HTTP调用和断言
前置条件: (1) 上下文环境中必须存在Gherkin .feature文件。(2) 项目必须使用Spring Boot。对于与框架无关的Java项目,请使用@133-java-testing-acceptance-tests。
范围: 仅实现正常路径场景。请参考文档获取详细示例和约束条件。

Constraints

约束条件

Before applying any acceptance test changes, ensure the Gherkin .feature file is in context and the project compiles. If compilation fails or the feature file is missing, stop immediately.
  • PRECONDITION: The Gherkin .feature file MUST be in context; the project MUST use Spring Boot
  • MANDATORY: Run
    ./mvnw compile
    or
    mvn compile
    before applying any change
  • SAFETY: If compilation fails, stop immediately and do not proceed
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed examples, good/bad patterns, and constraints
在应用任何验收测试变更前,确保Gherkin .feature文件存在且项目可编译。若编译失败或feature文件缺失,请立即停止操作。
  • 前置条件:上下文环境中必须存在Gherkin .feature文件;项目必须使用Spring Boot
  • 强制要求:在应用任何变更前运行
    ./mvnw compile
    mvn compile
  • 安全要求:若编译失败,立即停止操作,不得继续
  • 验证:在应用改进后运行
    ./mvnw clean verify
    mvn clean verify
  • 应用前须知:阅读参考文档获取详细示例、优劣模式及约束条件

When to use this skill

何时使用本Skill

  • Review Java code for Spring Boot acceptance tests
  • Apply best practices for Spring Boot acceptance tests in Java code
  • 评审Java代码中的Spring Boot验收测试
  • 在Java代码中应用Spring Boot验收测试最佳实践

Reference

参考文档

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