shrusthip
shrusthip

Reputation: 95

Best way to validate entire Json response in JMeter

I would like to know best way to verify entire Json response in Jmeter. Can somebody help. Thanks.

Upvotes: 1

Views: 1393

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

The easiest way is use JSR223 Assertion and JsonSlurper class like:

  1. Put the anticipated JSON into expected JMeter Variable using i.e. User Defined Variables test element like:

    JMeter User Defined Vairables

  2. Add JSR223 Assertion as a child of the request you want to validate
  3. Put the following code into "Script" area:

    def expected = new groovy.json.JsonSlurper().parseText(vars.get('expected'))
    def actual = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    
    if (expected != actual) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Mismatch between expected and actual JSON')
    }
    
  4. If there will be any difference between expected and actual JSON - the sampler will be marked as failed

    JMeter JSR223 Assertion JSON

Upvotes: 3

Related Questions