Reputation: 417
I'm performing API testing of basic CRUD functionality. For the creation of each record type in each thread group, I am using the same BeanShell Assertion template customized to each thread group.
import org.apache.jmeter.services.FileServer;
if (ResponseCode != null && ResponseCode.equals("200") == true) {
SampleResult.setResponseOK();
}
else if (!ResponseCode.equals ("200") == true ) {
Failure = true;
FailureMessage ="Creation of a new {insert record type} record failed. Response code " + ResponseCode + "." ; // displays in Results Tree
print ("Creation of a new {insert record type} record failed: Response code " + ResponseCode + "."); // goes to stdout
log.warn("Creation of a new {insert record type} record failed: Response code " + ResponseCode); // this goes to the JMeter log file
// Static elements or calculations
part1 = "\n FAILED TO CREATE NEW {insert record type} RECORD via POST. The response code is: \"";
part2 = "\". \n\n - For \'Non-HTTP response code - org.apache.jorphan.util.JMeterStopThreadException\' is received,verify the payload file still exists. \n - For response code = 409, \n\t a) check the payload for validity.\n\t b) verify the same {insert record type} name doesn't already exist in the {insert table name} table. If found, delete record and re-run the test. \n - For response code = 500, verify the database and its host server are reachable. \n";
// Open File(s)
FileOutputStream f = new FileOutputStream(FileServer.getFileServer().getBaseDir() + "\\error.log", true);
//FileOutputStream f = new FileOutputStream("c:\\error.log", true);
PrintStream p = new PrintStream(f);
// Write data to file
p.println( part1 + ResponseCode + part2 );
// Close File(s)
p.close();
f.close();
}
Is there a way to make this re-callable as opposed to having to repeat it in each thread group? Right now I'm up to 20 thread groups, thus 20 versions of this same assertion.
I've looked at multiple pages on this site and also at How to Use BeanShell: JMeter's Favorite Built-in Component, but I'm not finding a solution to this. Any feedback is appreciated.
Upvotes: 1
Views: 145
Reputation: 167992
If you place your Assertion (any Assertion) at the same level as Thread Groups like:
It will be applied to both "Sampler 1" and "Sampler 2". Moreover, the assertion will be applied to each sampler in each thread group on each iteration.
See How to Use JMeter Assertions in Three Easy Steps guide which clarifies assertions scope, cost and best practices.
Upvotes: 2