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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Validate Responses in RESTful Web Service Performance Tests

How to Validate Responses in RESTful Web Service Performance Tests

Learn how to validate responses when performance testing RESTful web services.

Canberk Akduygu user avatar by
Canberk Akduygu
·
Nov. 09, 18 · Tutorial
Like (1)
Save
Tweet
Share
9.72K Views

Join the DZone community and get the full member experience.

Join For Free

Representational State Transfer (REST) is an architectural style that pre-defines a set of constraints to be used for creating web services. Web services that use REST architectural style, or RESTful web services, provide interoperability between any system on the internet. Nowadays many development teams are switching to restful APIs to simplify client and server communication. That’s why performance or stress testing of that web service is crucial.

By following this tutorial, you will be able to validate how many concurrent users your application’s server-side component can handle.

Apache JMeter is strong when it comes to assertions. You can assert the response state or directly to the response data. By using those assertions, you will be able to verify the stability of your application under heavy load. There are different options to make a validation.

  1. Response Assertion
  2. JSON Assertion
  3. JSR223 Assertion

Response Assertion

We talked about response assertions in our SOAP service blog post. You can use the same approach as you used in SOAP web service test as there’s no difference between their usage.

You can measure response times, validate response code or response messages.

JSON Assertion

In order to make a good JSON assertion, you need to know how to use JSONPath. You can use http://jsonpath.com/  to learn JsonPath.

During a performance test, our approach should be not only monitoring the performance of the application but also the integrity of the response data. So using JSON Extractors and JSON Assertions in one test would be a good fit for our need.

Our scenario is to fetch all books records, extract the id and title of a random book. Then we’ll search for this specific book. Finally, we’ll make an assertion to the response with extracted values.

First, we use JSON extractor component of Apache JMeter. It has 4 different fields

  • Names of created variable: We need to define variable names for extracted values. We’ll use those value by using the ${variableName} format during the test.
  • JSON Path expression: This should be obvious.
  • Match no: in case your JSON Expression return multiple values, this field works as an index for your response. In case you want to randomize the selection, you need to use 0 in that field.
  • Default Values: In case there’s no data in the response, you may define default values.

In case you want to extract multiple data in one action you need to separate your expression, variable by using “;”. By that way, you will be able to extract related data.

Then you need to use JSON Assertion component of JMeter. In this component, all you need to do is to give proper JSON expression to find the attribute and the value to compare.

Right now, your script will be able to compare the expected result and mark your test as pass or fail at the end of execution.

JSR223 Assertion

JSR223 support Groovy, Java or BeanShell script. This feature lets you go beyond the out of the box solution that JMeter provides.  You can manipulate the response or create custom assertions. That depends on your need and your imagination.

In this example, we extract the response data, extract the id and check if it’s null or empty. In case it validates those condition, it will fire a failure message with a custom failure message.

Groovy scripting is a must to use this feature.

import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(SampleResult.getResponseDataAsString());
String id = response.id.toString();

if(id == null || id.equalsIgnoreCase(“”)){
AssertionResult.setFailureMessage(“NO ID Found”);
AssertionResult.setFailure(true);
}
else
AssertionResult.setFailure(false);

You can always use Loadium to run your performance test in the cloud.

Happy load testing to your RESTful services.

REST Web Protocols Web Service Testing Assertion (software development) JSON

Published at DZone with permission of Canberk Akduygu. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Brief Overview of the Spring Cloud Framework
  • ChatGPT Prompts for Agile Practitioners
  • Top 5 Node.js REST API Frameworks
  • Top 10 Secure Coding Practices Every Developer Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: