简体   繁体   English

Spring Cloud Contract 在合约文件中使用 bodyFromFile 生成弱测试

[英]Spring Cloud Contract generates weak tests using bodyFromFile in contract file

I try to implement simple contract for get users REST Endpoit.我尝试为get users REST Endpoit 实施简单的合同。 I expect that Spring Cloud Contract generates test whitch checks json response more strongly.我希望 Spring Cloud Contract 生成测试 whitch 更强烈地检查 json 响应。 Can anyone know how to configure test generator?enter code here谁能知道如何配置测试生成器?在此处输入代码
My contract yaml file here:我的合同yaml文件在这里:

  Get Users contract
request:
  method: GET
  url: /users
  headers:
    Content-Type: application/json
response:
  status: 200
  bodyFromFile: get_users_response.json
  headers:
    Content-Type: application/json

and get_users_response.json :get_users_response.json

[
  {
    "id": 1,
    "name": "User1"
  },
  {
    "id": 2,
    "name": "User2"
  }
]

Spring Cloud Contract generates: Spring 云合约生成:

...
assertThatJson(parsedJson).array().contains("['id']").isEqualTo(1);
assertThatJson(parsedJson).array().contains("['name']").isEqualTo("User1");
assertThatJson(parsedJson).array().contains("['id']").isEqualTo(2);
assertThatJson(parsedJson).array().contains("['name']").isEqualTo("User2");
...

But I assumed something like this:但我假设是这样的:

//check the correspondent element
assertThatJson(parsedJson).elementWithIndex(0).field("['id']").isEqualTo(1);
assertThatJson(parsedJson).elementWithIndex(0).field("['name']").isEqualTo("");
assertThatJson(parsedJson).elementWithIndex(1).field("['id']").isEqualTo(2);
assertThatJson(parsedJson).elementWithIndex(1).field("[name']").isEqualTo(1);
//and check array size
assertThatJson(parsedJson).array().hasSize(2);

You can turn on the array size check.您可以打开数组大小检查。 Check the docs https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/project-features.html#contract-limitations .检查文档https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/project-features.html#contract-limitations For your convenience I'm copying the part of the docs为了您的方便,我正在复制文档的一部分

The support for verifying the size of JSON arrays is experimental.支持验证 JSON arrays 的大小是实验性的。 If you want to turn it on, set the value of the following system property to true: spring.cloud.contract.verifier.assert.size.如果要打开它,请将以下系统属性的值设置为 true:spring.cloud.contract.verifier.assert.size。 By default, this feature is set to false.默认情况下,此功能设置为 false。 You can also set the assertJsonSize property in the plugin configuration.您还可以在插件配置中设置 assertJsonSize 属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用spring-cloud-contract-oa3和spring cloud contract gradle插件时无法生成合约测试 - Unable to generate contract tests when using spring-cloud-contract-oa3 and spring cloud contract gradle plugin 生成的测试在Spring Cloud Contract中失败 - Generated tests fail in Spring Cloud Contract Spring Cloud Contract regex(nonBlank())生成控制符号 - Spring cloud contract regex(nonBlank()) generates control symbols 如何在 spring-cloud-gateway 合约测试中从 spring-cloud-contract 中设置带有 StubRunner 端口的 url - How to set urls with port of StubRunner from spring-cloud-contract in spring-cloud-gateway contract tests Spring Cloud Contract 测试是否应该实际调用外部服务? - Should Spring Cloud Contract tests actually call an external service? Spring Cloud Contract 测试适用于 Maven,但不适用于 JUnit - Spring Cloud Contract tests work with Maven but not when run with JUnit Spring Cloud合约测试的在线/离线模式存在问题 - Problems with the online / offline mode of the Spring Cloud contract tests Spring 云合约:基于合约的测试根本不会在生产者端生成 - Spring cloud contract: tests based on contracts are not generated on producer side at all 如何在不构建项目中的所有单元和合同测试的情况下生成和运行单个合同测试? Java Spring Cloud 合约验证器 - How to generate and run a single contract test without building all the unit and contract tests in the project? Java Spring Cloud Contract Verifier Spring Cloud Contract和Webflux路由 - Spring cloud contract and webflux routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM