Jmeter - Beanshell 断言中的内联评估错误

Jmeter - Inline evaluation error in Beanshell Assertion

我收到此错误,无法弄清楚我在做什么错:

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.

这是我正在使用的代码:

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

谁能帮我解决这个问题?一直卡在这上面。

您似乎忘记了从第 12 行开始以分号结束所有语句。添加分号,让我知道它是如何工作的!

  1. 需要这样加分号

    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. 你的脚本可以简化为

    AssertionResult.setFailure(vars.get('x_api_key') == vars.get('apiKeyRec'))
    
  4. 而且你甚至不需要任何脚本来比较 2 个变量,它可以使用“正常”来完成 Response Assertion