Search code examples
jmeterbeanshelljmeter-5.0

Jmeter - Inline evaluation error in Beanshell Assertion


I am getting this error, and cannot figure out what wrong I am doing:

Error invoking bsh method: eval In file: inline evaluation of: ``import java.util.Set; import java.util.Map; import java.util.List;  try { //   Map . . . '' Encountered "String" at line 17, column 9.

This is the code that I am using:

import java.util.Set;
import java.util.Map;
import java.util.List;

try
{
//  Map<String,List<String>> map = new HashMap<String,List<String>>();

//  map = vars.getObject("headerMap");
    boolean isHeaderValid = false;
    
//  String apiKeySent = "${x_api_key}"
//  String clientIdSent = "${X_IBM_Client_id}"
//  String clientSecretSent = "${X_IBM_Client_Secret}"

    String apiKeySent = vars.get("x_api_key")
    String clientIdSent = vars.get("X_Client_id")
    String clientSecretSent = vars.get("X_Client_Secret")

    log.info("apiKeySent: " + vars.get("x_api_key"))
    log.info("clientIdSent: " + vars.get("X_Client_id"))
    log.info("clientSecretSent: " + vars.get("X_Client_Secret"))
    
    if(apiKeySent != "")
    {
        apiKeyRec = vars.get("apiKeyRec")
        isHeaderValid = apiKeySent.equals(apiKeyRec)
    }
    Failure = isHeaderValid
}
catch(Exception e)
{
    log.debug("Error in verification: ",e)
}

Could anyone please help me in figuring this out? Have been stuck at this for ages.


Solution

    1. You need to add semicolons like this

      import java.util.Set;
      import java.util.Map;
      import java.util.List;
      
      try
      {
      //  Map<String,List<String>> map = new HashMap<String,List<String>>();
      
      //  map = vars.getObject("headerMap");
          boolean isHeaderValid = false;
      
      //  String apiKeySent = "${x_api_key}"
      //  String clientIdSent = "${X_IBM_Client_id}"
      //  String clientSecretSent = "${X_IBM_Client_Secret}"
      
          String apiKeySent = vars.get("x_api_key");
          String clientIdSent = vars.get("X_Client_id");
          String clientSecretSent = vars.get("X_Client_Secret");
      
          log.info("apiKeySent: " + vars.get("x_api_key"));
          log.info("clientIdSent: " + vars.get("X_Client_id"));
          log.info("clientSecretSent: " + vars.get("X_Client_Secret"));
      
          if(apiKeySent != "")
          {
              apiKeyRec = vars.get("apiKeyRec");
              isHeaderValid = apiKeySent.equals(apiKeyRec);
          }
          Failure = isHeaderValid;
      }
      catch(Exception e)
      {
          log.debug("Error in verification: ",e);
      }
      
    2. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion and Groovy

    3. Your script can be simplified to

      AssertionResult.setFailure(vars.get('x_api_key') == vars.get('apiKeyRec'))
      
    4. And you don't even need any scripting for comparing 2 variables, it can be done using "normal" Response Assertion

      enter image description here