I try to validate a json response (just like in the example below from the website), but I fail on validation optional elements. I can only validate mandatory elements with 'validate', because if the json-path failes, it already counts as failed.
Can anybody get me a hint on how to validate a value, IF AND ONLY IF the element exists? If it does not exist (optional), it is good also...
Example if have to validate : The description is optional, but if it is set, it should not be longer than 30 characters.
http()
.client(todoClient)
.receive()
.response(HttpStatus.OK)
.messageType(MessageType.JSON)
.validate("$.id", "${todoId}")
.validate("$.title", "${todoName}")
.validate("$.description", "${todoDescription}");
May be this post can help you, there is the use of Matchers.
https://stackoverflow.com/a/50817190/11770752
First you can try Matchers.hasValue
, if so you check with Matchers.hasSize
.
Otherwise i found the .jsonPath(String jsonPathExpression, Object controlValue)
after .messageType(MessageType.JSON)
but i don't know if it does check for optionals or even validates the Json Value.
EDIT: As is was browsing the Documentation there is the option of Validation Matchers in Citrus. http://citrusframework.org/reference/html/index.html#validation-matcher
Replace "${todoDescription}"
with "@matches('\\w{30}')@"
, it should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.