Abhishek Asthana Abhishek Asthana - 1 month ago 9
Groovy Question

Groovy using a different value from what was initialized

In my JMeter script i have defined a User Defined Variable i whose value is set to 1. Then i created a JSR22 pre-processor for my HTTP sample that just increments the value of i using the below code.

log.info(vars.get("i"));

Integer intI = vars.get("i");

intI = intI + 1;

vars.put("i", intI.toString());

log.info(vars.get("i"))


The problem seems to be that instead of picking up 1 as the initial value of i it is picking 49 and increment it to 50.

I was able to fix it by changing

Integer intId = vars.get("id")


to

Integer intId = vars.get("id").toInteger();


But i am curious to know what could be causing this.

Answer

As requested, the conclusion from the comments as answer:

vars.get() returns a String which, when assigned to an Integer gets implicitly converted to the corresponding ASCII value.

This can be observed in the snippet: def v = "1"; Integer intId = v; println intId -> prints 49