vlatko606
vlatko606

Reputation: 1149

Jmeter - How to fail the http request with certain message into the response using JSR223 Assertion

While testing my API, and When something is wrong in the request I got messages like:

{
    "errorTimeStamp": "2021-10-21T14:08:11.7298099+02:00",
    "errorMessage": "Ticket with id 0 not found",
    "errorCode": 1001,
    "hasError": true
}

How to mark the httpRequest as failed(red into View Results Tree) based on: "hasError": true

I prefer to use JSR223 Assertion sampler, so later I can extend and include other matchers

Upvotes: 0

Views: 246

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

Something like:

def hasError = new groovy.json.JsonSlurper().parse(prev.getResponseData()).hasError

if (hasError) {
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage('Response data has error')
}

should do the trick for you

More information:

Upvotes: 2

Related Questions