为 spring 云合同测试编写 groovy 合同

Writing groovy contract for spring cloud contract testing

我是合同测试的新手,想为响应编写一个 groovy 合同,其中包含带有字典字符串列表等的子对象。 我有这样的回复:

{
    "data": [
      {
        "categories": [
          {
            "categories": [],
            "oid": "abc",
            "type": "xyz"
          },
          {
            "categories": [],
            "oid": "abb",
            "type": "xyy"
          }
        ],
        "oid": "ab"
      }
    ],
    "meta": {
      "datatype": "text",
      "language": "x",
      "market": "qw",
      "provider": "AFP",
      "status": "ok",
      "statusInfo": {},
      "supportedLanguages": [
        "x"
      ]
    }
  }

为此,我编写了以下合同:

Contract.make {
    request {
        method 'GET'
        url '/foo'
    }
    response {
        status 200
        body(
                 "data"  : [
                     (
                        "categories": [
                            (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyz"
                            ),
                            (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyy"
                            )
                        ]
                        "oid" : regex('\w')
                     )
                 ],
                 "meta" : (
                    "datatype": "text",
                    "language": "x",
                    "market": "qw",
                    "provider": "AFP",
                    "status": "ok",
                    "statusInfo": (),
                    "supportedLanguages": ["x"]
                 )
        )
        headers {
            contentType(applicationJson())
        }
    }
}

但无法正常工作。 你能帮我知道我在这里做错了什么吗?

您的合同无效。您在应该使用括号的地方使用了括号

Contract.make {
    request {
        method 'GET'
        url '/foo'
    }
    response {
        status 200
        body(
                 "data"  : [
                     (
                        "categories": [
                            [ // was (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyz"
                            ], // was )
                            [ // was (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyy"
                            ] // was )
                        ]
                        "oid" : regex('\w')
                     )
                 ],
                 "meta" : [ // was (
                    "datatype": "text",
                    "language": "x",
                    "market": "qw",
                    "provider": "AFP",
                    "status": "ok",
                    "statusInfo": [:], // was ()
                    "supportedLanguages": ["x"]
                 ] // was )
        )
        headers {
            contentType(applicationJson())
        }
    }
}