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);
}

You can also try this code with Online Java Compiler
Run Code
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);
}

You can also try this code with Online Java Compiler
Run Code
Here, you can see that most of the configurations of the request are the same for both test cases. Look at the highlighted part.

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.


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!