You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot提供者测试能否像存根测试一样使用_data文件夹JSON示例?

问题

在运行Spring Boot API提供者测试时,希望像存根测试一样使用带有JSON示例的_data文件夹,操作步骤如下:

  • 在src/test/contracts路径下创建了quote-api.yaml契约文件;
  • 创建src/test/contracts/quote-api_data文件夹,并放入happy_path.json文件,内容如下:
{
  "http-request": {
    "method": "POST",
    "path"  : "/quote",
    "body"  : {
      "quoteReference": "VALID_QUOTE_REFERENCE",
      "customers": [
        {
          "customerType": "Z",
          "dateOfBirth" : "2000-01-01"
        },
        {
          "customerType": "Z",
          "dateOfBirth" : "2001-01-01"
        }
      ]
    }
  },
  "http-response": {
    "status" : 200,
    "body" : {
      "fullPrice": 10.0,
      "monthlyPrice": 20.0,
      "quoteStatus": "OK"
    }
  }
}
  • 编写了ContractTests类,代码如下:
public class ContractTest extends SpecmaticJUnitSupport {
  private static ConfigurableApplicationContext context;

  @BeforeAll
  public static void setUp() {
    System.setProperty("host", "localhost");
    System.setProperty("port", "8080");
    System.setProperty("contractPaths", "src\\test\\contracts\\quotes.yaml");

//    System.setProperty("SPECMATIC_GENERATIVE_TESTS", "true");

    context = SpringApplication.run(HomeownerapiApplication.class);
  }

  @AfterAll
  public static void tearDown() {
    context.close();
  }
}

但在IntelliJ中运行ContractTests时,happy_path.json文件未被识别。请问:

  1. 是否必须使用.spec文件来外部化提供者测试的示例?
  2. 如果是,上述示例中的customer数组该如何表示?
回答

1. 是否必须使用.spec文件?

是的,在Specmatic的提供者测试中,外部化测试示例需要放在.spec文件中——_data文件夹下的JSON示例仅适用于存根模式,不能被提供者测试识别。运行提供者测试时,Specmatic只会读取.spec文件中的示例来验证API实现。

2. 如何在.spec文件中表示customer数组?

你需要把happy_path.json的示例内容迁移到.spec文件中,使用Specmatic的契约语法定义。以下是两种实现方式:

方式一:单独创建.spec文件引用契约

# src/test/contracts/quote-api.spec
contract: quote-api.yaml

scenarios:
  - name: happy_path
    request:
      method: POST
      path: /quote
      body:
        quoteReference: "VALID_QUOTE_REFERENCE"
        customers:
          - customerType: "Z"
            dateOfBirth: "2000-01-01"
          - customerType: "Z"
            dateOfBirth: "2001-01-01"
    response:
      status: 200
      body:
        fullPrice: 10.0
        monthlyPrice: 20.0
        quoteStatus: "OK"

方式二:在原yaml契约中内嵌示例

# src/test/contracts/quote-api.yaml
openapi: 3.0.0
info:
  title: Quote API
  version: 1.0.0
paths:
  /quote:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                quoteReference:
                  type: string
                customers:
                  type: array
                  items:
                    type: object
                    properties:
                      customerType:
                        type: string
                      dateOfBirth:
                        type: string
                        format: date
              required: [quoteReference, customers]
      responses:
        '200':
          description: Success response
          content:
            application/json:
              schema:
                type: object
                properties:
                  fullPrice:
                    type: number
                  monthlyPrice:
                    type: number
                  quoteStatus:
                    type: string
              examples:
                happy_path:
                  value:
                    fullPrice: 10.0
                    monthlyPrice: 20.0
                    quoteStatus: "OK"
      x-specmatic-scenarios:
        - name: happy_path
          request:
            body:
              quoteReference: "VALID_QUOTE_REFERENCE"
              customers:
                - customerType: "Z"
                  dateOfBirth: "2000-01-01"
                - customerType: "Z"
                  dateOfBirth: "2001-01-01"

额外注意事项

  • 调整contractPaths系统属性指向正确的文件:如果使用单独的.spec文件,需改为src/test/contracts/quote-api.spec;
  • 若要启用生成式测试,取消注释SPECMATIC_GENERATIVE_TESTS=true配置,Specmatic会基于契约结构自动生成更多测试用例,同时执行你定义的scenarios示例。

内容的提问来源于stack exchange,提问作者Mark Gargan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 22:03:30