How to print the values of variables in a JMeter bean shell assertion

Udhay picture Udhay · Jul 28, 2014 · Viewed 37k times · Source

Using Jmeter, I'm passing values to a webservice through a REST API. On Success the API updates the values to a mongo DB. While Asserting using JMeter BeanShell Assertion..I want to display the values sent in the Request and values Stored in the DB.

Im Using the below Script..

String req_data="${Request_Configuration}";
String res_data="${mongo_db_Configuration}";


if(res_data.equalsIgnoreCase(req_data)){
   Failure=false;
   FailureMessage = "Data stored in DB is correct";
   System.out.println("ReqData="+req_data);
   System.out.println("ResData="+res_data);
}
else{
   Failure = true;
   FailureMessage = "Data Stored in DB is NOT correct";
   System.out.println("ReqData="+req_data);
   System.out.println("ResData="+res_data);
}

Im Just not able to Print ReqData and ResData. Please help out.

Answer

Dmitri T picture Dmitri T · Jul 28, 2014

You have a problem in your script. In Beanshell you cannot access variables like ${Request_Configuration}, you need to use vars.get("Request_Configuration") instead.

vars is a shorthand for JMeterVariables class instance for current context.

So your Beanshell Assertion code should look as follows:

String req_data=vars.get("Request_Configuration");
String res_data=vars.get("mongo_db_Configuration");


if(res_data.equalsIgnoreCase(req_data)){
   Failure=false;
   FailureMessage = "Data stored in DB is correct";
   System.out.println("ReqData="+req_data);
   System.out.println("ResData="+res_data);
}
else{
   Failure = true;
   FailureMessage = "Data Stored in DB is NOT correct";
   System.out.println("ReqData="+req_data);
   System.out.println("ResData="+res_data);
}

I would also suggest using log.info() instead of System.out.println() as in that case results will go to jmeter.log file and won't be "eaten" by exceeding screen buffer size.

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting and various JMeter API objects exposed to Beanshell explanation.