Search code examples
jmeterassertionjmeter-pluginsbeanshell

How to get the Response Message and Response Code when the Assertion Fails in jmeter


I am working on Webservice Testing using Jmeter.I am using Beanshell Assertion In this I am trying to display the Response Code and Response Message but the problem is when the Request Fails I am unable to get the response Code|Message displayed on my jmeter log file.In case If I get the Proper response I am getting the Response Code|Message Properly.Is there any solution to get the Response Code|Message when the Request Fails in jmeter BeanShell Assertion


Solution

  • Perhaps something is wrong with your Beanshell code as Beanshell Assertion should normally handle parent sampler failure

    Beanshell Assertion example

    The code which produced these log lines looks as simple as:

    StringBuilder sb = new StringBuilder();
    sb.append("Sampler: ");
    sb.append(SampleResult.getSampleLabel()).append(" ");
    sb.append("Successful: ");
    sb.append(SampleResult.isSuccessful()).append(" ");
    sb.append("Code: ");
    sb.append(ResponseCode).append(" ");
    sb.append("Message: ");
    sb.append(ResponseMessage);
    
    log.info(sb.toString());
    

    Few tips:

    1. Add debug(); statement as 1st line of your Beanshell Assertion and inspect STDOUT for details
    2. Surround your code in try/catch block and in the catch block print exception to jmeter.log file as

      try {
          //your Beanshell Assertion code here
      }
      catch (Throwable ex) {
          log.error("Something went wrong", ex)
      }
      
    3. See How to use BeanShell: JMeter's favorite built-in component guide for details on proper Beanshell scripting in JMeter.