Using BeanShell Assertion for saving failed response

1.5k Views Asked by At

I'm trying to save the response of failed requests into log but unsuccessfully. I have HTTP requests which each one has its own Response assertion, and once in a while through my load test a response doesn't meet the requirements of the Response Assertion, so I want to fail it. I tried to add this code to the BeanShell Assertion following this post:

if (Boolean.valueOf(vars.get("DEBUG"))) {
  for (a: SampleResult.getAssertionResults()) {
    if (a.isError() || a.isFailure()) {
      log.error(Thread.currentThread().getName()+": "+SampleLabel+": Assertion failed for response: " + new String((byte[]) ResponseData));
    }
  }
}

The Test plan itself looks like this:

enter image description here

In user defined variables I've added the following: Name: DEBUG Value: (none)

I ran this program through the non GUI mode - cmd and on purpose made a response fail (response assertion failed it) but I did not get any error response logged to my log file. What am I doing wrong? thanks

2

There are 2 best solutions below

1
On BEST ANSWER

I would rather let JMeter do this automatically, it is the matter of a couple of properties, to wit:

  1. First of all, you need to tell JMeter to store its results in XML format:

    jmeter.save.saveservice.output_format=xml
    
  2. Second, you can amend the following property values to configure JMeter to store assertion failure message and data into .jtl results file

    jmeter.save.saveservice.assertion_results_failure_message=true
    jmeter.save.saveservice.assertion_results=all
    
  3. Probably the most useful option: simply save response data for failed samplers:

    jmeter.save.saveservice.response_data.on_error=true
    

The above properties can be either put into user.properties file (located in JMeter's "bin" folder, JMeter restart is required to pick the changes up) or you can pass them via -J command line argument like:

jmeter -Jjmeter.save.saveservice.output_format=xml -Jjmeter.save.saveservice.response_data.on_error=true ....

See Apache JMeter Properties Customization Guide for more information on working with JMeter Properties to tune various aspects of JMeter configuration

0
On

First of all if your goal is to just save failed responses, together with assertion results, you don't need any custom code. You can add Simple Data Writer configured like this:

enter image description here

Option 2: If you want to write responses into the log file, then you need custom script. But in that case I'd suggest using BeanShell Listener (unlike assertions and other objects, there's only 1 instance of listener for all threads, so it's more economical, and less chance to create a bottleneck at log writing).

Also according to your script, the value of DEBUG must be true, but as you mentioned, you did not set it to true (empty string will be resolved to false by Java's valueOf). So change it to

Name: DEBUG 
Value: true

Here's how such BeanShell Listener's script looks:

if (Boolean.valueOf(vars.get("DEBUG"))) {
  for (a: sampleResult.getAssertionResults()) {
    if (a.isError() || a.isFailure()) {
      log.error(sampleResult.getThreadName() + ": " 
                + sampleResult.getSampleLabel() + ": Assertion failed for response: " 
                + sampleResult.getResponseDataAsString());
    }
  }
}

Option 3 is to stay with your original solution, then you need to fix the variable assignment (as I mentioned before).