Heavy users of JMeter, like me, sometimes experience difficulties when you’re creating functional JMeter tests. JMeter not only has its raison d’être for performance and load testing, but even for quite complex automated functional tests against web services. And as tester you need to test against requirements, let’s just have a small example:
Your web service has a delete method (eg. deleteData) that only set’s a flag for deletion, there’s no physical deletion, just a logical one. If you try to alter the data after a delete (with editData), you must get an error and there must be no data change at all.
Given you deleted a record with deleteData().
When you try to alter the data with editData().
Then you get an status error response.
Most commonly you have managed to set some global assertions in your JMeter test plan to catch basic errors like non-200-HTTTP status or SOAP-Faults, exceptions et cetera.
These global assertions will then prevent you from correctly executing negative tests, tests that have an internal error as result or throw an exception. Here’s a solution by using a Beanshell Postprocessor and setting all response values in the code to adopt to the expected states, a nice workaround. Just add this code in the Beanshell Postprocessor under your request sampler:
String response = new String(data);
boolean found = response.contains("Expected pattern"); // in this example we search for an expected pattern in the response data
// unexpected -> failed validation
String errormsg = "Value was unexpected!";
prev.setStopTestNow(true); // stop test
// as expected -> passed validation
String errormsg = "Everything as expected!";