Groovy using a different value from what was initialized

时间:2016-10-20 18:38:06

标签: groovy jmeter

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.

1 个答案:

答案 0 :(得分:0)

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 (if the String is exactly 1 character, otherwise it will cause a runtime exception).

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