Spring Cloud Contract Verifier - 身体匹配

时间:2018-04-17 09:43:59

标签: spring-boot spring-cloud spring-cloud-contract

我正在使用Spring Cloud Contract Verifier

org.springframework.cloud:spring-cloud-starter-contract-verifier:jar:2.0.0.M8 org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.4.RELEASE

我有以下合同:

request:
  method: GET
  url: /cars/list
response:
  status: 200
  headers:
    Content-Type: application/json;charset=UTF-8
  body:
    cars:
      [
        {
          "make": "Ford",
          "model": "Fiesta",
          "year": 2016,
          "price": 16500.50
        },
        {
          "make": "BMW",
          "model": "X1",
          "year": 2014,
          "price": 22000.00
        },
        {
          "make": "NISSAN",
          "model": "Juke",
          "year": 2017,
          "price": 19300.00
        }
      ]

然后在以下java代码中转换:

DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).array("['cars']").contains("['model']").isEqualTo("Juke");
assertThatJson(parsedJson).array("['cars']").contains("['make']").isEqualTo("Ford");
assertThatJson(parsedJson).array("['cars']").contains("['year']").isEqualTo(2014);
assertThatJson(parsedJson).array("['cars']").contains("['year']").isEqualTo(2016);
assertThatJson(parsedJson).array("['cars']").contains("['model']").isEqualTo("Fiesta");
assertThatJson(parsedJson).array("['cars']").contains("['make']").isEqualTo("BMW");
assertThatJson(parsedJson).array("['cars']").contains("['year']").isEqualTo(2017);
assertThatJson(parsedJson).array("['cars']").contains("['make']").isEqualTo("NISSAN");
assertThatJson(parsedJson).array("['cars']").contains("['price']").isEqualTo(16500.5);
assertThatJson(parsedJson).array("['cars']").contains("['model']").isEqualTo("X1");
assertThatJson(parsedJson).array("['cars']").contains("['price']").isEqualTo(22000.0);
assertThatJson(parsedJson).array("['cars']").contains("['price']").isEqualTo(19300.0);

我对此测试有一些问题:

1)它没有考虑到顺序,这意味着以下主体仍然被认为是有效的:

{
  "cars": [
    {
      "make": "NISSAN",
      "model": "Juke",
      "year": 2017,
      "price": 19300.00
    },
    {
      "make": "BMW",
      "model": "X1",
      "year": 2014,
      "price": 22000.00
    },
    {
      "make": "Ford",
      "model": "Fiesta",
      "year": 2016,
      "price": 16500.50
    }
  ]
}

2)它只是验证某些属性是否存在于响应中的任何位置,这意味着以下正文仍被视为有效:

{
  "cars": [
    {
      "make": "Ford",
      "model": "Juke",
      "year": 2016,
      "price": 19300.00
    },
    {
      "make": "BMW",
      "model": "X1",
      "year": 2017,
      "price": 22000.00
    },
    {
      "make": "NISSAN",
      "model": "Fiesta",
      "year": 2014,
      "price": 16500.50
    }
  ]
}

3)它忽略了响应中存在的任何额外值,这意味着以下正文仍然被视为有效:

{
  "cars": [
    {
      "make": "Ford",
      "model": "Fiesta",
      "year": 2017,
      "price": 22000.00,
      "state": "good"
    },
    {
      "make": "NISSAN",
      "model": "Juke",
      "year": 2016,
      "price": 19300.00,
      "state": "ok"
    },
    {
      "make": "BMW",
      "model": "X1",
      "year": 2014,
      "price": 16500.50
    },
    {
      "make": "Volkswagen",
      "model": "Golf",
      "year": 2018,
      "price": 12500.00,
      "state": "new"
    }
  ]
}

我希望生成的测试更加严格,并且在上述情况中概述的任何差异都会失败。

可以通过Spring Cloud Contract Verifier实现吗?

1 个答案:

答案 0 :(得分:1)

首先,您的版本不匹配

org.springframework.cloud:spring-cloud-starter-contract-verifier:jar:2.0.0.M8 org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.4.RELEASE

您使用的版本与验证程序不同。那些应该匹配。

现在提出问题

  

1)它没有考虑到顺序,这意味着以下主体仍然被认为是有效的:

是的,我们不会开箱即用。您可以通过为整个身体提供自己的JSON路径匹配器来手动完成(https://cloud.spring.io/spring-cloud-contract/single/spring-cloud-contract.html#contract-matchers

  

2)它只是验证某些属性是否存在于响应中的任何位置,这意味着以下正文仍被视为有效:

是的,我们断言结构是好的,这是合同测试的想法。如果您想要更精确的验证,请使用匹配器部分(https://cloud.spring.io/spring-cloud-contract/single/spring-cloud-contract.html#contract-matchers

  

3)它忽略了响应中存在的任何额外值,这意味着以下正文仍然被视为有效:

这绝对是你应该做的。忽略未知字段。您可以在此处阅读有关Postel法律的更多信息(https://en.wikipedia.org/wiki/Robustness_principle

  

我希望生成的测试更加严格,并且在上述情况中概述的任何差异都会失败。

我认为您正在寻找架构而不是合同测试或定义。您可以使用Spring Rest Docs集成(https://cloud.spring.io/spring-cloud-contract/single/spring-cloud-contract.html#_generating_stubs_using_rest_docs)并在那里强制执行存根的确切外观。但是IMO(当然我不知道你的域名)你的限制性太强了。