Sadha Nanda
Sadha Nanda

Reputation: 357

How to add Assertion in JSR223 using java

How to add assertion on response message in jsr223 postprocessor using java code.

I tried using AssertionResult.setFailure(true); but it is not working

Upvotes: 3

Views: 2949

Answers (2)

Ori Marko
Ori Marko

Reputation: 58862

Use JSR223 Assertion instead JSR223 PostProcessor

JSR223 Assertion allows JSR223 script code to be used to check the status of the previous sample.

You are missing calling setFailureMessage

AssertionResult.setFailureMessage("Your reason for failure");

The script can check various aspects of the SampleResult. If an error is detected, the script should use AssertionResult.setFailureMessage("message") and AssertionResult.setFailure(true).

Upvotes: 2

Dmitri T
Dmitri T

Reputation: 168157

Something like:

def expectedMessage = 'some expected message'
def actualMessage = prev.getResponseMessage()

if (expectedMessage != actualMessage) {
    AssertionResult.setFailure(true)
}

Should do the trick for you, in the above example prev stands for previous SampleResult

Demo:

enter image description here

More information: Scripting JMeter Assertions in Groovy - A Tutorial

Also get used to look at jmeter.log file, it might be the case your script fails somewhere somehow

Upvotes: 0

Related Questions