Magnifying Glass

Custom assertions in Jmeter

Recently I needed load test an HTTP endpoint to verify:

  1. A few simple fields match expected values.
  2. Length of an array somewhere inside the JSON response matches the expected length.

The first check is trivial in Jmeter with a ‘Json Assertion’

The second one was a little more tricky because the Json Assertion doesn’t support the length() operator. Luckily the ‘BeanShell Assertion’ comes to the rescue. This type of assertion lets us write our own logic to decide the pass/fail outcome.

Here’s a short example:

import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;

Object aStringWithJson = new String(SampleResult.getResponseDataAsString());
List items = JsonPath.read(aStringWithJson , "$.path.to.my.array", new Filter[] {});
int expectedSize = Integer.parseInt(Parameters.trim());
int actualSize = items.size();
if( actualSize != expectedSize ) {
    Failure = true;
    FailureMessage = "Got '" + actualSize + "' expected '" + expectedSize + "'";
}

The script has access to a bunch of interesting global variables:

Variable Meaning
SampleResult The result of the request.
Parameters the parameter provided to the assertion in the GUI.
Failure Setting it to true causes Jmeter to fail the request/response pair.
FailureMessage Jmeter reports this string a the reason why the request/response pair failed.