Dummy Dummy - 1 year ago 258
Groovy Question

JMeter Assertion Results

I am running JSR223 Assertion where I compare expected response to actual response using groovy. If they are not equal, assertion fails.
Here is the code

import groovy.json.JsonSlurper;

JsonSlurper slurper = new JsonSlurper();
boolean set_assertion = true;

def expected_response = slurper.parseText("<JSON response goes here>");
def actual_response = slurper.parseText(prev.getResponseDataAsString());

if (expected_response != actual_response)
set_assertion = false;
log.error("expected_response != actual_response");
assert set_assertion == true;

Now, assertion does fail. But I think it fails because I get internal error in the logs.
Here is my Assertion result.
Assertion Results
Here is the logs

ERROR - jmeter.threads.JMeterThread: Error processing Assertion Assertion failed:

assert set_assertion == true
| |
false false

at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:402)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:650)
at Script48.run(Script48.groovy:16)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:352)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:153)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:206)
at org.apache.jmeter.assertions.JSR223Assertion.getResult(JSR223Assertion.java:47)
at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:755)
at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:746)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:410)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:241)
at java.lang.Thread.run(Unknown Source)

Ideally I should not have errors, and my assertion should fail and should have the following results 'Assertion error:false' and 'Assertion failure:true'.

Could you please help me understand where I am making a mistake. My most concern is the error in the logs. Thank you!

Answer Source

Here is what's happening:

You're using Groovy "assert". As below comparison is false:

expected_response != actual_response

You end up failing on:

assert set_assertion == true;

This throws org.codehaus.groovy.runtime.powerassert.PowerAssertionError which is a subclass or java.lang.Error which is trapped here:

So JMeter executes this code which is what you get:

        assertionResult = new AssertionResult("Assertion failed! See log file.");

This seems fine to me, but why use Groovy assert here ? it will be much more efficient to just do this:

 if ( expected_response != actual_response )
      log.error("expected_response != actual_response");
      AssertionResult.setFailureMessage("expected_response != actual_response");

You can learn more on assertions reading this blog.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download