0
votes

I want compare two values and pick which one is equal to second variable. I have written code like below in BeanShellPostProcessor

HitID = vars.get("AddPrpc139");
b=139
if(HitID.equals(b))
{
log.info("......value=");
}else
{
    log.info("......value=");
}

But i am getting below error

2018-11-27 14:48:53,504 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: `` HitID = vars.get("AddPrpc139"); b=139 if(HitID.equals(b)) { log.info("......val . . . '' Encountered "if" at line 4, column 1.

2018-11-27 14:48:53,504 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: `` HitID = vars.get("AddPrpc139"); b=139 if(HitID.equals(b)) { log.info("......val . . . '' Encountered "if" at line 4, column 1.

2

2 Answers

0
votes

Java/Beanshell expect ; at end of the line. Also b can be inlined

 HitID = vars.get("AddPrpc139");
 if("139".equals(HitIDb))
 {

Also consider moving to JSR223 PostProcessor

0
votes
  1. You need to follow Java syntax rules, to wit you need to add a semicolon after b=139 line.
  2. You should also surround this 139 with quotation marks otherwise JMeter will be comparing a String with an Integer and you will always get into else branch even if the values will be the same

Amended code:

HitID = vars.get("AddPrpc139");
b = "139";
if (HitID.equals(b)) {
    log.info("Values are equal, expected: " + b + ", got: " + HitID);
} else {
    log.info("Values are NOT equal, expected: " + b + ", got: " + HitID);
}'

Demo:

enter image description here


Be aware that according to JMeter Best Practices you should be using JSR223 PostProcessor with Groovy language starting from JMeter 3.1. Groovy is more modern language, it is compatible with latest Java features and it has much better performance. Check out Apache Groovy - Why and How You Should Use It article for more details.