32

I am using JMeter as a unit test tool, in parameterised calls where I expect some of the responses to be 500 internal server errors. I am using BeanShell Assertions to check the responses.

I want some of the 500 internal server errors to NOT be marked as failures if the response contains a specified text. All 500 server errors are marked as failures. Is it possible to change the behavior?

Below is an extract from the BeanShell Assertion.

if (ResponseCode.equals("500")) {
    Failure = false;
    String respData = new String(ResponseData);

    if (! respData.contains("specific Text")) {
        Failure = true;
        FailureMessage = "500 Internal Server Error:  Unexpected Response.   " + 
            "Response Message: " + respData;
    }
}  

Thank you and Regards

4 Answers 4

57

Another possible solution is to use Response Assertion with checked "Ignore Status" flag added to your sampler:

Ignore status
Instructs JMeter to set the status to success initially.

The overall success of the sample is determined by combining the result of the assertion with the existing Response status. When the Ignore Status checkbox is selected, the Response status is forced to successful before evaluating the Assertion.

HTTP Responses with statuses in the 4xx and 5xx ranges are normally regarded as unsuccessful. The "Ignore status" checkbox can be used to set the status successful before performing further checks. Note that this will have the effect of clearing any previous assertion failures, so make sure that this is only set on the first assertion.

3
  • 3
    This is exactly what I needed. I don't want to change the response to OK as suggested in your other answer... I want to assert that the response is the correct error (in my case, 404 Not Found). So, thanks!
    – James Dunn
    Oct 21, 2013 at 16:51
  • 2
    Perfect. I needed to check for 401. Thank you!
    – asokan
    Jul 10, 2014 at 18:11
  • 1
    This is the perfect answer. Saved my time. Thank you.
    – Prabesh
    Jan 16, 2018 at 19:12
40

UPD: please find most simple & "native" solution below:


In case if you want to do some tricky things in code use the following approach.

Access and modify SampleResult to change the status from "FAIL" to "PASS" if the code is 500 from your JSR223 Assertion or use JSR223 PostProcessor instead - they all have access to SampleResult Object.

1. JSR223 Assertion

if (ResponseCode.equals("500") == true) { 
    SampleResult.setResponseOK();  

    /* the same is 
    SampleResult.setSuccessful(true);
    SampleResult.setResponseCodeOK();
    SampleResult.setResponseMessageOK();
    */
}

2. JSR223 PostProcessor
Use prev instead - to access SampleResult object of the sampler to which one post-processor is attached:

if (prev.getResponseCode().equals("500") == true) { 
    prev.setResponseOK();  

    /* the same is 
    prev.setSuccessful(true);
    prev.setResponseCodeOK();
    prev.setResponseMessageOK();
    */
}
3
  • Thank you. Adding SampleResult.setResponseOK(); worked perfectly for what I needed. Mar 21, 2012 at 12:31
  • @Gazen Ganados, it would be very kindly to accept answer in this case. Mar 21, 2012 at 14:47
  • 3
    This changes the response code. your other answer is a much better solution imo
    – MishaP
    Nov 13, 2014 at 23:17
1

Add a "Response Assertion" after the request you want to pass and the check the "Ignore Status" check box.

-2

Using a BeanShell assertion, force the HTTP Sampler to pass and then pass/fail on a regular Beanshell assertion statement instead:

if (ResponseCode.equals("500") == true) { 
    SampleResult.setResponseOK();  

    /* the same is 
    SampleResult.setSuccessful(true);
    SampleResult.setResponseCodeOK();
    SampleResult.setResponseMessageOK();
    */
}
String path = SampleResult.getURL().getPath();
if (!path.contains("anerror")) {
    Failure = true;
    FailureMessage = "URL Path: didn't contain \"anerror\"" +
        System.getProperty("line.separator") + "URL Path detected: " + path;
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.