Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Specification in REST Assured
3.
RequestSpecification in REST Assured
3.1.
Example
4.
Frequently Asked Questions
4.1.
What is RequestSpecBuilder?
4.2.
What is the difference between RequestSpecBuilder and RequestSpecification?
4.3.
What are the request specs?
4.4.
What is the use of a request builder?
4.5.
What is a put request in API?
5.
Conclusion
Last Updated: Mar 27, 2024

REST Assured –RequestSpecification – How the request will look like

Author Amit Singh
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Have you worked on testing some APIs? Or have you used multiple test cases with some common configurations again and again?

logo

This article focuses on one of the API testing tools, which is Rest Assured. We will also study specifications in Rest Assured. We will mainly concentrate on RequestSpecification in REST Assured. Let’s see all these things in more detail.

Specification in REST Assured

You can use specification objects to avoid duplicate request parameters and responses in case you are working on multiple tests. You can use the same object every time you make a different request. You have to do it in case of numerous RESTful requests and response validations.

Following are the two types of specification builder Java classes:

  1. RequestSpecification (RequestSpecBuilder)
     
  2. ResponseSpecification (ResponseSpecBuilder)
     
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

RequestSpecification in REST Assured

Rest Assured provides the interface of the RequestSpecification. You can use it to club and retrieve repetitive actions. The action can be anything like setting up the base URL, HTTP verbs, headers, etc. These actions may be identical for multiple calls of Rest.

You can use the retrieved common code in different requests. It reduces the number of lines in the code. This eventually helps in increasing maintainability. 

Let's try to understand this with an example.

Example

Now, let’s move on to an example. In this example, let's assume that we have two test cases. These test cases will help in testing an API endpoint. 

Test case 1:

@Test
public void test1() {
    RestAssured.given()
    .baseUri("http://appBaseURL/endPoint1")
    .when().get().then()
    .statusCode(200)
    .header("contentType","application/Json")
    .contentType(ContentType.JSON);
}

 

Test case 2:

@Test
public void test2() {
    RestAssured.given()
    .baseUri("http://appBaseURL/endPoint2")
    .when().get().then()
    .statusCode(200)
    .header("contentType","application/Json")
    .contentType(ContentType.JSON);
}


Here, you can see that most of the configurations of the request are the same for both test cases. Look at the highlighted part.

common

 

You can see that even though these request configurations are the same for both test cases, they are still duplicated. Let's assume that we will be working in an exhaustive test suite. In that case, it will become quite haptic to write these codes again and again. These codes will be hard to maintain if you are thinking of exchanging some data across all these test cases.  

In these types of situations, we can use RequestSpecification as a rescuer. We can extract these common test parameters or configurations as separate entities. Now, we can use these separate entities in all our test cases. You can easily understand this in the following image.

responsespec

updated

 

It would be best if you check out the above image. In the picture, you can see that we have taken the common request setup. We have put this setup in a separate base test case. We are doing this using the RequestSpecification interface. Now, we can use this setup in actual test cases. This helps us overcome redundancy. It also enhances the maintainability and reusability of the code. 

Frequently Asked Questions

What is RequestSpecBuilder?

RequestSpecBuilder is a class in Rest Assured. It contains methods that help in setting cookies, multipart details, header, body, authentication, query parameters, form parameters, path parameters, base URI, base path, proxy, etc.

What is the difference between RequestSpecBuilder and RequestSpecification?

You can use the RequestSpecBuilder to make a RequestSpecification. But RequestSpecification helps you to specify how that request will look like.

What are the request specs?

Request specs help you to focus on a single controller action. But unlike controller tests, it includes the router and the middleware stack. It also consists of both rack requests and responses.

What is the use of a request builder?

We can use a request builder to configure the per-request state. We can also configure the request URI, the request method, specific request headers, etc. The default request method is GET unless it is explicitly set. Each of the setter methods modifies or changes the state of the builder and then returns the same instance.

What is a put request in API?

The HTTP PUT request method helps you to create a new resource. You can also replace a representation of the target resource. You can do it with the request payload.

Conclusion

In this article, we have studied the RequestSpecification in REST Assured in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding the REST Assured and if you would like to learn more, check out our articles on building RequestSpecification and Setup basic REST Assured project.

Check out this problem - Duplicate Subtree In Binary Tree

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Previous article
REST Assured – Writing Response in a Text File
Next article
Default Host And Port, Get and Assert in Rest Assured
Live masterclass