问题
I'm writing api test automated codes with bdd method by Karate.
Assertion part pushes me hard. My response is like below, and when I called that path $.data.subscribers[0].products
this gives me empty-null array as I checked by http://jsonpath.herokuapp.com/ website. However, when I run my gherkin Karate feature then It gave me the response as null, It still gives me error assertion. It says actual:
null, expected: null...
I couldn't see any error, how can I fix this? Is it a bug or Is there any method to handle it?
My response:
{
"meta": {
"return_code": 0,
"message": "success"
},
"data": {
"sbs": [
{
"sbs_id": 32432432,
"dt": "OTT",
"pt": []
},
{
"sbs_id": 455654445,
"dt": "IPTV",
"pt": []
}
]
}
}
JsonPath that I wrote:
$.data.sbs[0].pt[0]
Assertion that Karate gave:
com.intuit.karate.exception.KarateException: base-tvpp-cases.feature:316
- path: $[0], actual: null, expected: null, reason: actual json-path does
not exist
Gherkin-Karate- code that I wrote in feature file: I tried both below
And match response.data.subscribers[0].products[0] == null
And match response.data.subscribers[0].products[0] == '#null'
回答1:
Just use a JsonPath wildcard (..
or *
). Note that when you use a wildcard - the result will always be a JSON array. Here is your solution:
* match response.data..subscribers[0].products[0] == []
来源:https://stackoverflow.com/questions/55606379/assertion-issue-of-jsonpath-response