2

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

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

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Sadha Nanda
  • 337
  • 4
  • 15

2 Answers2

2

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).

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

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

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Hi Bro thanks for your reply but i am looking solution for Java not for Groovy – Sadha Nanda Apr 20 '21 at 02:58
  • 1
    There is no `Java` in JSR223 Assertion, what you think of "java" is [Beanshell](http://www.beanshell.org/) and [according to JMeter Best Practices you should not be using it for scripting](https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting). More information: [Apache Groovy - Why and How You Should Use It](https://www.blazemeter.com/blog/groovy-new-black) – Dmitri T Apr 20 '21 at 04:44