Next Practical Step
Audit your test plan: replace global Thread-Group-level assertions with targeted child assertions, and change Response Assertion rules from “Matches” to “Substring”.
Comprehensive guide to JMeter assertions: Response, JSON Path, Duration, Size, XPath2, and JSR223 assertions with performance and SLA best practices.
Assertions in Apache JMeter validate that responses received from the target server conform to expected status codes, response times, headers, and payload structures. When an assertion fails, JMeter marks the sample as failed (red), records the failure message, and triggers configured error handling rules.
This guide covers assertion execution hierarchy, deep dives into the most critical assertion types, custom JSR223 assertions, and performance best practices under high concurrency.
| Assertion Type | Target | CPU / Memory Overhead | Best For |
|---|---|---|---|
| Response Assertion | Status code, headers, URL, body | Lowest (native string / regex) | HTTP 200, status messages, text matching |
| JSON Assertion | REST JSON response payloads | Low (JSONPath evaluation) | Validating specific fields, arrays, or object properties |
| Duration Assertion | Response time in milliseconds | Negligible (integer comparison) | Strict SLA latency enforcement |
| Size Assertion | Byte size of response | Negligible (byte comparison) | Empty body or minimum payload length checks |
| XPath2 Assertion | XML / SOAP responses | Medium (DOM / SAX parsing) | Complex XML tree validation |
| JSR223 Assertion | Dynamic / multi-condition rules | Low (if Groovy cached) | Cross-field validation, database verification |
The Response Assertion tests fields of the response using substring, pattern matching, or equality.
Text Response (Response body)Response Code (HTTP status code, e.g., 200, 201)Response Message (e.g., OK, Created)Response Headers (e.g., Content-Type: application/json)Request Headers / URL SampledField to Test: Response CodePattern Matching Rule: EqualsPatterns to Test: 200Used for REST APIs returning application/json. It evaluates expressions using JSONPath syntax.
$.data.user.id or $.items[?(@.price > 100)]null.| Pattern | Meaning | Example Match |
|---|---|---|
$.status | Top-level property | "SUCCESS" |
$.data.items.length() | Array element count | 5 |
$.items[0].id | First item ID in array | 101 |
$.users[*].email | Array of all emails | ["a@b.com", "c@d.com"] |
$.orders[?(@.total > 500)] | Filters orders with total > 500 | Non-empty array |
Enforces a hard maximum response time limit. If prev.getTime() > Duration (ms), the sampler fails with:
The test took too long: 1240 ms, max allowed 1000 ms.
Duration in milliseconds: 1000Validates response size in bytes:
=, !=, >, <, >=, <=.When built-in assertions cannot express complex business logic (e.g., verifying mathematical calculations, token expiration times, or cross-variable consistency), use a JSR223 Assertion.
In a JSR223 Assertion, use AssertionResult to signal success or failure:
import groovy.json.JsonSlurper
// Parse responseString response = prev.getResponseDataAsString()if (!response) { AssertionResult.setFailure(true) AssertionResult.setFailureMessage("Response body was completely empty.") return}
try { def json = new JsonSlurper().parseText(response)
// Validate schema field if (json.status != "ACTIVE") { AssertionResult.setFailure(true) AssertionResult.setFailureMessage("Expected status ACTIVE but got: ${json.status}") return }
// Cross-validate with JMeter variable String expectedUserId = vars.get("expectedUserId") if (json.user?.id?.toString() != expectedUserId) { AssertionResult.setFailure(true) AssertionResult.setFailureMessage("User ID mismatch: expected ${expectedUserId}, got ${json.user?.id}") return }
// SLA check combined with business validation if (prev.getTime() > 2000) { AssertionResult.setFailure(true) AssertionResult.setFailureMessage("SLA breach: request took ${prev.getTime()}ms (limit 2000ms)") }} catch (Exception e) { AssertionResult.setFailure(true) AssertionResult.setFailureMessage("Invalid JSON format: " + e.getMessage())}\${var} interpolation.