jmeter: evaluate the values of JSON values

197 Views Asked by At

I am trying to test my API response using JSON assertion in JMeter, but couldn't find out on how to achieve it. The API returns 2 values, and I need to check if the difference between these two value are consistent

API response:

{
  "start": "12759898",
  "end": "12759907"
}

enter image description here

I've tried like the above, but it seems to be wrong, as its a JSONPath variable. Could anyone guide on how to evaluate these values? is it possible to achieve this?

1

There are 1 best solutions below

0
On BEST ANSWER

It looks like a job for JSR223 Assertion

  1. Add JSR223 Assertion as a child of the request which returns the above JSON
  2. Put the following code into "Script" area:

    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    def start = response.start as long
    def end = response.end as long
    def delta = end - start
    
    if (delta != 10) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Expected: 10, got: ' + delta)
    }
    

    If the difference between start and end will not be equal to 10 - the request will be marked as failed.

    enter image description here

More information: