JSON Path Assertion fails to validate the path

652 views
Skip to first unread message

mlshiv...@gmail.com

unread,
May 21, 2017, 9:56:02 AM5/21/17
to jmeter-plugins
Hi,

   For your reference I have attached the .jmx. The test plan contains a Dummy Sampler which returns a response data in JSON format. I have JSON Path Assertion as a child for the dummy sampler.

The JSON response looks as:
{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}}}

My expectation is to fail the JSON Path Assertion by giving the value as : "$..book[1].doc" in the "JSON Path:" field of "JSON Path Assertion".
As the Path ("$..book[1].doc") is not available in the response, the assertion should fail. But in the contrary the assertion passed.

Could any one help me on this?

Thanks and Regards,
Shiva Prasad.

json-path-postprocessor.jmx

gli...@gmail.com

unread,
May 29, 2017, 10:23:00 AM5/29/17
to jmeter-plugins, mlshiv...@gmail.com
Out of interest, why didn't you tick Validate Against Expected Value box? As per documentation:

Third, if JSON path was found in the document, and validation against expected value was requested, it will perform validation. 

If it remains unchecked the assertion will fail only if the JSON Path expression won't match anything at all, empty matches are considered as success. 


Just in case see How to Use JMeter Assertions in Three Easy Steps article. 


mlshiv...@gmail.com

unread,
May 31, 2017, 12:46:33 AM5/31/17
to jmeter-plugins, mlshiv...@gmail.com, gli...@gmail.com
Yes, the assertion fails if I check the Validate Against Expected Value box. But the second point in the documentations states 
"Second, it will search for specified path, using syntax from Jayway JsonPath 1.2.0. If the path is not found, it will fail.
Here in my case the path "$..book[1].doc" don't exists at all.

       Third point also states that "If it remains unchecked the assertion will fail only if the JSON Path expression won't match anything at all". Please guide me if I misunderstood the documentation.

Many thanks for the reply.

gli...@gmail.com

unread,
Jun 1, 2017, 5:17:09 AM6/1/17
to jmeter-plugins, mlshiv...@gmail.com, gli...@gmail.com
Well, it looks like a bug in the JSON Path Assertion, I would recommend reporting it via jmeter-plugins discussion

In the meantime you can work it around via JSR223 Assertion, the relevant Groovy code would be something like:

def response = com.jayway.jsonpath.JsonPath.parse(prev.getResponseDataAsString()).read('$..book[1].doc')
if (response.size() == 0) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Required JSON structure is not found')
}

References:

mlshiv...@gmail.com

unread,
Jun 2, 2017, 12:37:27 AM6/2/17
to jmeter-plugins, mlshiv...@gmail.com, gli...@gmail.com
Hi Glinius,

              Thank you very much for the alternate solution and for quick reply.
Regards,
Shiva Prasad
Reply all
Reply to author
Forward
0 new messages