Search code examples
jmeterbeanshell

Jmeter BeanShell - compare between int to counter


I'm trying to compare int (parse from string) to counter in BeanShell assertion.

my code:

int i = Integer.parseInt(vars.get("count_2"));

counter = vars.get("counter");


if (i != counter)


{

 Failure = true;
 FailureMessage = "failed";

} else {

    Failure = false;
}

On debug sampler I can see that both "count_2" and "counter" have the same values in all loop runs, but the assertion fails.

What went wrong?


Solution

  • Option 1: use integers everywhere

    Change this line:

    counter = vars.get("counter");
    

    To:

    int counter = Integer.parseInt(vars.get("counter"));
    

    Option 2: use strings everywhere

    String  i = vars.get("count_2");
    
    String counter = vars.get("counter");
    
    if (!i.equals(counter))
    ...
    

    JMeterVariables can be either Strings or Objects, so you need to cast them to the types you need to work with.

    See How to use BeanShell: JMeter's favorite built-in component guide for essential information on scripting in JMeter and some form of cookbook.