Automation Testing

REST-assured How to Check Response Times

By Test Guild
  • Share:
Join the Guild for FREE

In the past, I’ve written a few posts on how to get started using Rest Assured. Today I wanted to go over a new feature — measure response time — that was just introduced in the REST Assured 2.8.0 version released on December 12.

This might not seem like a big change, but for my team, it is, because we sometimes have requirements that need to meet a certain time threshold. Until now we had to create our own function to do this but the build in REST-assured version is much more elegant and easier to use than our implementation.


So let’s take a look at how to use REST-assured to measure response times from a REST service.  For this example, I’ll be using my favorite REST API to work with Google Books.


We are going to work with this request and verify that the response returns the first title element value Alan Turing. We are also going to make sure that the response returns in the expected time.

REST-assuredResponseTime

  • First, create a new Maven Java project and make sure to add the rest-assured 2.8.0 dependency info in your pom.file
  • Next, create a new class called ResponseTime
  • Import the following
import org.junit.Test;
import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.get;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;

  • Create a method simpleExample with a jUnit @Test annotation
  • Enter the following:
String json = get("volumes?q=turing").asString();
System.out.println(json);
    
given().
parameters("q","turing").
when().
get("volumes").
then().
body("items.volumeInfo.title[1]", equalTo("Alan Turing") ).
and().time(lessThan(10L));

  • Run the test
  • The test should fail with the message


“java.lang.AssertionError: 1 expectation failed.  Expected response time was not a value less than <10L> milliseconds, was 765 milliseconds (765 milliseconds).”

  • Cool once we know that it’s working; enter the value 1000 milliseconds and rerun. This time it should now pass.

Here is the full code for the ResponseTIme Class:

import org.junit.Test;
import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.get;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;

public class ResponseTIme {
 
 @Test
 public void simpleExample(){
  
  RestAssured.baseURI = "https://www.googleapis.com/books/v1/";
  
  String json = get("volumes?q=turing").asString();
  System.out.println(json);
    
  given().
    parameters("q","turing").
  when().
    get("volumes").
  then().
    body("items.volumeInfo.title[1]", equalTo("Alan Turing") ).
    and().time(lessThan(1000L));
    
 }
 
}


More Rest-Assured Info


For more info on the new rest-assured response time feature and all the other cool new features check out

https://github.com/jayway/rest-assured/wiki/Usage#measuring-response-time

 

REST-ASSURED – HOW TO POST A JSON REQUEST

REST-assured How to Check Response Times

REST Testing with Java Part Two: Getting Started with Rest-Assured

A bearded man with blue glasses and a black-and-white jacket smiles at a microphone in a studio setting.

About Joe Colantonio

Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.

Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.

With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.

He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.

Follow him on LinkedIn or check out more at TestGuild.com.

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

What is Regression Testing? (2025 Regression Test Guide)

Posted on 03/30/2025

What is Regression Testing ? Regression testing ensures that new changes—whether they’re bug ...

Why Enterprise Test Automation is Broken—And How to Fix It

Posted on 03/21/2025

Why is Enterprise Test Automation So Hard? Enterprise software testing should accelerate releases, ...

Exploring the Value of AI in Test Case Creation (Pros and Cons)

Posted on 03/10/2025

As testers, we're used to balancing test coverage with tight project deadlines. Right? ...