3

How to handle negative cases in JMETER, for example my expected output response is 400("There are no records") for an GET API?

In JMETER response is coming as failure or warning.

Is JMeter only handle positive scenarios like for all GET API response code should be 200 ?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233

3 Answers3

2
  1. Add Response Assertion as a child of the HTTP Request sampler which returns HTTP Status Code 400
  2. Configure it as follows:

    • Tick Ignore status box
    • Set "Field to test" to Response code
    • Set "Pattern matching rules" to Equals
    • Add 400 as a "Pattern to test"

      enter image description here

This way JMeter will pass only if the parent HTTP Request sampler returns 400 status code, otherwise it will fail.

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

You can add to HTTP Request Response Assertion with Ignore status checked

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.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Thanks for your response @user7294900, but still my doubt is not clear, response code 400 is expected for me , so i want to result as green but as i understood get API only shown green when status code came as 200 for GET API. I have added ignore status as 400 is it correct ? – Avnish Rathore Dec 26 '19 at 11:53
  • @AvnishRathore you can either ignore status or choose Response Code radio button and enter Pattern to Test as 400, to allow only 400 to be consider successful – Ori Marko Dec 26 '19 at 13:15
0

I tried with this, by adding a BeanShell Assertion with following code.

import org.apache.jmeter.assertions.AssertionResult;
String failureMessage = "";
String ResCode = SampleResult.getResponseCode();
if (!ResCode.equals("400")) {
    failureMessage = "Got Response Code" + ResCode;
    AssertionResult result = new AssertionResult("Expected Response 400");
    result.setFailure(true);
    result.setFailureMessage(failureMessage);
    prev.addAssertionResult(result);
    prev.setSuccessful(false);
    SampleResult.setStartNextThreadLoop(true);
} else {
 //failure criteria
}

enter image description here

Jyoti Prakash Mallick
  • 2,119
  • 3
  • 21
  • 38