Search code examples
jmeterjsonpathjson-path-expressionjsonassert

jmeter: evaluate the values of JSON values


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?


Solution

  • 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: