Ariel Bell is the support engineer for BlazeMeter. He has 7 years of experience in technical support. He specializes in SaaS products and communication infrastructure.

Become a JMeter and Continuous Testing Pro

Start Learning

Test Your Website Performance NOW! |

arrowPlease enter a URL with http(s)
Jun 01 2017

Scripting JMeter Assertions in Groovy - A Tutorial

What are JMeter Assertions?


Apache JMeter™ assertions are components that enable developers to set criteria determining whether a response will be considered a “pass” or a “fail”.  You can use assertions to ensure returned values from samples and subsamples match expected results, and you can also apply them to JMeter Variables. They will be executed after every sampler in the same scope.


Assertions can be applied to either the main sample, the sub-samples or both. For example, if you request the page and want to determine if the server response was successful, you can use the Response Assertions to verify if the server response body includes the string ‘I'm Feeling Lucky’.


Using Groovy for Advanced JMeter Assertions


JMeter includes quite a few assertion elements for validating the sampler’s response.


jmeter assertions


However, sometimes your validation decision might follow complex and advanced logic, and can’t be configured using the out-of-the-box JMeter assertions. For example, confirming the validity of a JSON response, followed by evaluation of the values of the response, and having a customized failure message that will help debug the issue.


The Apache Groovy scripting language is great for writing concise and maintainable tests, and for all your build and automation tasks. Groovy has a seamless and transparent integration with Java and any third-party libraries, making it easy to use for Java developers who use JMeter. Features such as Power Assertion make testing and asserting easy and quick in Groovy, compared to any other scripting language available, such as BeanShell.


Asserting JSON with Groovy


In the following example, we want to test that the server response includes a well structured JSON. We will run a request through our application server, and we expect to receive a structured JSON that includes at least the 3 of the following keys: api_version, error and result.


Additionally, there is a key named “numberOfExpectedServers” inside “result”, and we expect it to be equal to 3.


Since we know which keys we are expecting to receive in the response, and the value that should be assigned to them, we can use a Groovy assertion to assert the response.


In the beginning of the assertion, we will first want to verify that the response was successful, so we will check that the response code is equal to 200. Then, we will show a step-by-step of how to code this example.


1. Start with a simple script that includes a Thread Group and a Sampler. I named the Sampler “Choose flight”.


2. Add the assertion element that provides us with the ability to use Groovy as our scripting language: JSR223


Right click on our Sampler -> Add -> Assertions -> JSR223 Assertion


jmeter groovy test scenario


3. In the element configuration part, set the following property: Script Language: Groovy 2.x.x.


jmeter groovy assertion configuration

JSR223 Assertion Fields Explained:

  • Name: Element name
  • Language: The scripting language to be used (Groovy, BeanShell, JS, etc.)
  • Parameters: The parameters to pass to the script. The parameters are stored in the following variables: Parameters, args
  • File Name: A path to a pre-made script file. Will override any script that is written in the main script field
  • Script compilation caching: When enabling this option, the JSR223 assertion (or any other JSR223 element for that matter) can pre-compile the code and cache it. This will greatly increase performance. ‘If available’ means that it will only apply to JSR223 compatible scripts. Java, JavaScript and Beanshell can be used in the JSR223 assertion, however they are not compatible with JSR223’s interface, as opposed to Groovy.


Script compilation caching is the major advantage of using JSR223 elements, as opposed to Beanshell elements.


4. You can now use a premade script (from the Script file section), or write a script from scratch.


Let’s look at this script, written from scratch. (To learn more about how to script in Groovy, click here).


import groovy.json.JsonSlurper;

def failureMessage = "";
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper ();

try {
    jsonResponse = JSON.parseText(prev.getResponseDataAsString());
    } catch (Exception e) {
    failureMessage += "Invalid JSON.\n"

	failureMessage += "Expected <response code> [200] but we got instead [" + prev.getResponseCode() + "]\n\n" ;

if (!jsonResponse.keySet().containsAll(["api_version","error","result"] )) {
          failureMessage += "The json config element has wrong structure.\n\n";

if (!jsonResponse.result.numberOfExpectedServers != 3) {
	failureMessage += "The element has wrong number of servers. Number of servers is:[" + jsonResponse.result.numberOfExpectedServers + "]\n\n";

if (failureMessage?.trim()) {

// Print error messages if any
if (failureMessage?.trim()) {
    failureMessage += "URL: " + SampleResult.getURL() + "\n\n";     
    failureMessage += "JSON RESPONSE: " + jsonResponse + "\n\n";
    failureMessage += "REQUEST HEADERS: " + SampleResult.getRequestHeaders() + "\n\n";



The script does the following:

  • Checks if the response code is equal to 200
  • Checks if the first level of the JSON contains the following keys: api_version, error, result
  • Checks if the key result numberOfExpectedServers is equal to 3.


If any of the conditions fail, the failure message will be added as a string. Therefore, if the failure message is empty, the assertion was successful. If it’s not empty, the assertion s set to fail and to display a failure message.


The script starts with importing the Json Slurper. The Json Slurper parses JSON text or reader content into Groovy data structures. Read more here.


5. Run the script and display the results in a listener.


An unsuccessful response:


jmeter groovy assertion unsuccessful response


In this response, the number of servers is 2, and not 3.


By using Blazemeter, a load testing tool that enhances JMeter abilities, we designed our “Errors” report to fit this sort of usage. If the assertion fails, the “Errors” report will help you understand the root-cause of the failure. This makes the debugging process easier.


As you can see, it’s clear that the test had a failed element, and any details that were passed to the assertion failure Message would be listed in this table.


errors report blazmeter


Want to learn more? You might be interested in viewing my webinar, Advanced JMeter Scripting - Writing Assertions in Groovy.


To learn more about JMeter check out this free 5 day online course.


Click here to subscribe to our newsletter.


To start testing with BlazeMeter, just out your URL or JMX file in the box below, and your test will start in minutes.

arrowPlease enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!