'Spring Cloud Contract generates weak tests using bodyFromFile in contract file
I try to implement simple contract for get users REST Endpoit. I expect that Spring Cloud Contract generates test whitch checks json response more strongly. Can anyone know how to configure test generator?enter code here
My contract yaml file here:
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:
[
{
"id": 1,
"name": "User1"
},
{
"id": 2,
"name": "User2"
}
]
Spring Cloud Contract generates:
...
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);
Solution 1:[1]
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 . For your convenience I'm copying the part of the docs
The support for verifying the size of JSON arrays is experimental. If you want to turn it on, set the value of the following system property to true: spring.cloud.contract.verifier.assert.size. By default, this feature is set to false. You can also set the assertJsonSize property in the plugin configuration.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marcin Grzejszczak |
