spring-cloud-contract
spring-cloud-contract copied to clipboard
escaped json string get expanded to wrong asserts in verification test
Given the contract definition
outputMessage {
body(file('response.json'))
}
response.json
"payload": "{\"propsA\":\"valueA\",\"propsB\":\"valueB\"}"
The above contract generates multiple asserts
assertThatJson(parsedJson).field("['payload']").field("['propsA']").isEqualTo("valueA");
assertThatJson(parsedJson).field("['payload']").field("['propsB']").isEqualTo("valueB");
The payload here is a json string, should be treated as String, but it wrongly treated as json object.
Can you provide content type to be application/json ? Also can you provide which version of contract you are using?
@marcingrzejszczak
this is for a message contract. Can I make content type to be application/json inside a outputMessage?
Yes. You can set it via messageHeaders
I just tried, makes no difference
outputMessage {
...
headers {
contentType(applicationJson())
}
}
still generates
assertThatJson(parsedJson).field("['payload']").field("['propsA']").isEqualTo("valueA");
assertThatJson(parsedJson).field("['payload']").field("['propsB']").isEqualTo("valueB");
Meh, I'll have to look into that.
With this commit https://github.com/spring-cloud/spring-cloud-contract/commit/a2444550d546f9f5663074d280475e72ed8cdf1d we're suggesting that whenever you have payload that contains an embedded JSON as part of your json, you should go with the fileAsBytes
approach and then you won't have any additional JSON assertions.