user1332137
user1332137

Reputation: 33

Complex response assertion in jmeter

I'm struggling a bit to build a proper test for this scenario: basically, I'm making a POST call to a web-service and trying to assert the following:

  1. A 201 response is ok
  2. A 409 response is also OK (but I extract this code to a variable and attempt a retry later)
  3. A 400 response might be OK, but only if there is a certain string in the response body

Items 1 and 2 above I got to work fine: I handle 2 with a Response Code Extractor and an If Controller later on.

My problem is: how do I test for a given substring on the response body, but only in the event of a 400 response code?

The final assertion I want to build goes something like this:

"(if the response code is 201 or 409) or (if the response code is 400 and 'substring' found in the body) then OK"

Upvotes: 0

Views: 628

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

I believe that you need to use Beanshell Assertion as it is the most flexible of all assertions provided.

Relevant Beanshell code will look as follows:

if (ResponseCode.equals("201") || ResponseCode.equals("409") || (ResponseCode.equals("400") && SampleResult.getResponseDataAsString().contains("something"))) {
    Failure = false;
}

See How to Use JMeter Assertions in 3 Easy Steps guide for more details on getting confidence by using JMeter Assertions.

Upvotes: 1

Related Questions