DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Demystifying Sorting Assertions With AssertJ
  • Why and How To Integrate Elastic APM in Apache JMeter
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts

Trending

  • Is Big Data Dying?
  • Using Python Libraries in Java
  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems
  • What’s Got Me Interested in OpenTelemetry—And Pursuing Certification
  1. DZone
  2. Coding
  3. Languages
  4. Scripting JMeter Assertions in Groovy - A Tutorial

Scripting JMeter Assertions in Groovy - A Tutorial

Learn to use JMeter™ assertions, which enable you to set the criteria to determine if a response will be considered a pass or fail in your load tests.

By 
Ariel Bell user avatar
Ariel Bell
·
Oct. 05, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.3K Views

Join the DZone community and get the full member experience.

Join For Free

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 www.google.com 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'.

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

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.

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

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


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).

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 is 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:

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.

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.

Assertion (software development) Groovy (programming language)

Published at DZone with permission of Ariel Bell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Demystifying Sorting Assertions With AssertJ
  • Why and How To Integrate Elastic APM in Apache JMeter
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!