I made this code so that if status == 500 the name of the api would be equal to "FAKE_CLIENT_RETRY", if the status of the api == "ERROR", the name would be equal to "FAKE_CLIENT_CALLBACK_ERROR"
import org.apache.jmeter.samplers.SampleResult;
//process main sample
if(("${status}").equals("500")) {
SampleResult.setResponseCodeOK();
SampleResult.setSuccessful(true);
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_RETRY");
}else if(("${status}").equals("ERROR")){
SampleResult.setSuccessful(false);
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_ERROR");
}else{
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_SUCESS");
}
But even when status == "ERROR" the name it returns is "FAKE_CLIENT_RETRY"
The strangest thing is that I know that the execution entered the "if" of the condition == "ERROR", because the return that comes with status == "ERROR" appears with execution failure in Jmeter and I forced the return in this case to return with fails via code snippet:
SampleResult.setSuccessful (false);
But despite having entered, it ignores the snippet that asks to rename the api.
Jmeter Sreenshot ----> Jmeter response
Following script worked without any issue within BeanShell Post Processor
if(("${status}").equals("500")) {
prev.setResponseCodeOK();
prev.setSuccessful(true);
prev.setSampleLabel("FAKE_CLIENT_RETRY");
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_RETRY");
}else if(("${status}").equals("ERROR")){
prev.setSuccessful(false);
prev.setSampleLabel("FAKE_CLIENT_CALLBACK_ERROR");
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_ERROR");
}else{
prev.setSampleLabel("FAKE_CLIENT_CALLBACK_SUCESS");
vars.put("Api_Fake_Client_Name","FAKE_CLIENT_CALLBACK_SUCESS");
}
Please note that you have access to the SampleResult through the variable previous
. Lets use prev.setSampleLabel("Label");
to set the label of the sample result.
prev - (SampleResult) - gives access to the previous SampleResult
Migration to JSR223 PostProcessor+Groovy is highly recommended for performance, support of new Java features and limited maintenance of the BeanShell library
Add a Debug Post Processor to view the JMeter variables through view result tree.
Sample test plan (JMX) is available in GitHub.