0

I've defined a variable in JMeter.

enter image description here

But when I try to access the variable from Groovy script it gives 53 instead of 5, if I put 10 it throws an exception

javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10' with class 'java.lang.String' to class 'int'

Here is my JSR223 Sampler script

int user_number = vars.get("USER_COUNT")
log.info('Total use is '+user_number)
2

Since vars.get("USER_COUNT") returns a String you cannot store that into a int variable that's the reason exception is thrown

You should convert the string to int using below way

int user_number = Integer.parseInt(vars.get("USER_COUNT"))
log.info('Total use is '+user_number)

Hope this will be helpful.

| improve this answer | |
2

vars is a shorthand for JMeterVariables and vars.get() function gives you a String

So the options are in:

  1. Use the string as it is:

    String user_number = vars.get("USER_COUNT")
    
  2. Or convert it to an Integer:

    int user_number = vars.get("USER_COUNT") as int
    
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.