As a part of End to End REST Assured Tutorial , in this post We will learn about “Get and assert response time of Request in Rest Assured”.

When a request is sent to a server, it responds with a response. The amount of time taken between sending a request to server and retrieving a response back form a server is called Response Time. An API must be faster. As a part of API testing, we must check the response time as well.

REST Assured provides a convenient way to retrieve and assert response time. Rest Assured provides method to get response time in milliseconds by default or the time unit we want. We can also validate if response time is less than , greater than or in between expected value as well.

There is a tricky part here:-

  1. If you just want to retrieve response time in milliseconds or other time units, you need to use time(), getTime(), timeIn(TimeUnit timeunit), getTimeIn( TimeUnit timeunit ) from Response interface. Response interface inherits these methods from ResponseOptions. You can not use Matchers in above methods.
  2. If you want to use Matchers i.e. assertion like response time is greater than a specific value, you need to use overloaded time() methods from ValidatableResponse which inherits time() method from ValidatableResponseOptions interface.

Getting confused? I feel understanding hierarchy is very much important otherwise you may not use correct method from correct class. Anyway I will explain with examples.

Interface ResponseOptions:-

This interface contains four methods :-

  1. getTime() – The response time in milliseconds (or -1 if no response time could be measured)
  2. getTimeIn(TimeUnit timeunit) – The response time in the given time unit (or -1 if no response time could be measured)
  3. time() – The response time in milliseconds (or -1 if no response time could be measured)
  4. timeIn( TimeUnit timeunit ) – The response time in the given time unit (or -1 if no response time could be measured)

Technically, getTime() and time() both are same and getTimeIn() and timeIn() both are same. Difference is Syntactic sugar.

Rest Assured Code:-

Interface ValidatableResponseOptions :-

This interface has overloaded time() methods which accepts Matcher.

  1. time(Matcher matcher) – Validate that the response time (in milliseconds) matches the supplied matcher.
  2. time(Matcher macther, TimeUnit timeunit) – Validate that the response time matches the supplied matcher and time unit.

Rest Assured Example:-

Complete code:-

You can clone/download example repo here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply