Adithya Balaji
Adithya Balaji

Reputation: 49

JMeter Assertion : Error when comparing variables

I am trying to assert my API response data with a value I obtained from my Database.

My code fails to compare my variables unless I add toString() to both my variable. Is there any way around it or is toString() mandatory?

The code in question is :

Boolean comparision = false;
for (int i; i < vars.getObject("dbData").size(); i++) {    
if (vars.getObject("dbData").get(i).get("DbData").toString().equals(${codeid_API}.toString()))
{
    comparision = true;            
}     
} 

${codeid_API} is the variable where I stored my API response data. (vars.getObject("dbData").get(i).get("DbData") gets the value from my DB.

Upvotes: 1

Views: 516

Answers (2)

Dmitri T
Dmitri T

Reputation: 168072

Don't inline JMeter variables in form of ${codeid_API} into Groovy scripts, in case of enabled compiled scripts caching it will be resolved only once and it might break your script logic.

Consider replacing it with vars.get('codeid_API) instead where vars is the shorthand for JMeterVariables class instance

Quote from JSR223 Sampler documentation:

JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.

props.get("START.HMS");

props.put("PROP1","1234");

More information: Debugging JDBC Sampler Results in JMeter

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58772

You can use Objects.equals instead

Objects.equals(vars.getObject("dbData").get(i).get("DbData"), ${codeid_API});

Returns true if the arguments are equal to each other and false otherwise.

For integer you can compare using == and use as int for casting

if (vars.getObject("dbData").get(i).get("DbData") as int == vars.get("codeid_API") as int );

Upvotes: 2

Related Questions