8

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.

Udhay
  • 93
  • 1
  • 2
  • 5
  • Why can't you print them? Does the output look odd or do you get an error? – Aaron Digulla Jul 28 '14 at 10:20
  • Request_Configuration : Value Sent in API Request , mongo_db_Configuration : Value fetched from mongodb using a MongoDB Query and processed using a BSF PostProcessor. – Udhay Jul 28 '14 at 10:21
  • The Assertion result says : Assertion error: false Assertion failure: true Assertion failure message: Data Stored in DB is NOT correct. But does not print the values in the Assertion Result. – Udhay Jul 28 '14 at 10:26
  • I don't see an assert in your code nor any "Assertion result". Please add the missing pieces to your question. – Aaron Digulla Jul 28 '14 at 10:28

2 Answers2

10

Use log.info()

Example

log.info("myVariable: " + vars.get("myVariable"));

My use case:

I did use the following code snipped in a BeanShell Assertion within my HTTP Request-sampler to print out my three variables id, type and value:

log.info(Thread.currentThread().getName()+": " + SampleLabel + ": id: " + vars.get("id"));
log.info(Thread.currentThread().getName()+": " + SampleLabel + ":  +-type: " + vars.get("type"));
log.info(Thread.currentThread().getName()+": " + SampleLabel + ":  +-value: " + vars.get("value"));

Printing also the built-in SampleLabel variable gives you the hint from which sampler you logged this information.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
9

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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133