[英]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.