🌐
Rest Assured Interview
import static io.restassured.RestAssured.*;
import io.restassured.response.Response;
Response response = get("https://api.example.com/resour
Question Banks ce");
1. What is REST Assured?
4. How do you validate a response using REST Assured?
REST Assured is a Java library designed for simplifying and automating
REST Assured provides various methods for response validation. You can
the testing of RESTful APIs. It provides a domain-specific language (DSL)
use the then on a Response object to apply assertions on status codes,
for writing readable and expressive tests for API endpoints, making it headers, response body, etc. For example:
easier to validate responses, headers, and more.
response.then().statusCode(200).contentType("applicatio
n/json");
2. How do you add REST Assured to a project?
To add REST Assured to a project, you can include its dependency in your
project's build configuration file, such as Maven or Gradle. For example, in 5. What is JSONPath in REST Assured?
Maven, you'd add the following dependency.
JSONPath is a query language used to traverse and query elements within
JSON responses. REST Assured allows you to use JSONPath to extract
specific data from JSON responses for validation or further processing.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
6. How do you extract data using JSONPath with REST Assured?
<version>4.4.0</version>
<scope>test</scope> In REST Assured, you can use the extract().jsonPath() method on a
</dependency> Responseobject to extract data using JSONPath expressions. For example:
String value = response.then().extract().jsonPath().get
3. How do you send GET requests using REST Assured? String("key.subKey");
To send a GET request using REST Assured, you can use the get method
from the RestAssured class, specifying the API endpoint. For example:
Rest Assured Interview Question Banks 1 Rest Assured Interview Question Banks 2
.when()
.delete("https://api.example.com/resource/{id}", res
7. How do you send POST requests using REST Assured?
ourceId)
To send a POST request using REST Assured, you can use the given() method .then()
to set request specifications and then use the post() method to send the .statusCode(204);
request. For example:
given()
.contentType("application/json") 9. What are Request Specifications in REST Assured?
.body(requestBody) Request Specifications in REST Assured are pre-defined configurations for
.when() requests. They can include headers, query parameters, authentication,
.post("https://api.example.com/resource") and more. Using request specifications helps maintain consistency and
.then() reusability across multiple requests.
.statusCode(201);
10. How do you authenticate using REST Assured?
REST Assured supports various authentication methods. For example, to
8. How do you send PUT and DELETE requests with REST Assured? perform basic authentication, you can use the auth() method along with
basic() or preemptive() authentication. For example:
Similar to POST requests, you can use the put() and delete() methods from
the RestAssured class to send PUT and DELETE requests. For example:
given()
.auth().basic("username", "password")
// Sending a PUT request
.when()
given()
.get("https://api.example.com/resource")
.contentType("application/json")
.then()
.body(requestBody)
.statusCode(200);
.when()
.put("https://api.example.com/resource/{id}", resour
ceId)
.then()
.statusCode(200); 11. What is BDD in REST Assured?
BDD in REST Assured refers to writing tests in a human-readable
// Sending a DELETE request
language that closely resembles natural language. It involves using
given()
Rest Assured Interview Question Banks 3 Rest Assured Interview Question Banks 4
methods like given() , when() , and then() to describe the behavior of the
javaCopy code
API under test, making tests more expressive.
ResponseSpec responseSpec = new ResponseSpecBuilder()
.expectStatusCode(200)
12. How do you perform query parameter validation in REST Assured? .expectContentType(ContentType.JSON)
.build();
You can use the queryParam() method within the given() section to add
query parameters to the request. For validation, you can use the then()
given()
section to assert the presence or value of the query parameter in the
.when()
response. For example:
.get("https://api.example.com/resource")
.then()
javaCopy code
.spec(responseSpec);
given()
.queryParam("key", "value")
.when()
.get("https://api.example.com/resource")
.then() 15. How do you handle timeouts in REST Assured?
.statusCode(200)
REST Assured allows you to set timeout values using the given() section. For
.body("queryParamkey", equalTo("expectedValue"));
example, you can use the timeouts() method to define the connection and
request timeouts. For instance:
javaCopy code
given()
13. What is Response Specification in REST Assured? .timeouts().connectTimeout(5000).readTimeout(5000)
.when()
Response Specification in REST Assured is a pre-defined configuration for
.get("https://api.example.com/resource");
response validation. It allows you to specify assertions and expectations
that are applied to the response, ensuring consistent validation across
multiple tests.
16. What is the "RootPath" feature in REST Assured?
14. How do you use Response Specification in REST Assured?
The "RootPath" feature in REST Assured allows you to specify a default
To use Response Specification, you can define it using the path that is used as the starting point for extracting data using JSONPath
ResponseSpecBuilder class and then apply it using the expect() method in the expressions. This can simplify data extraction when dealing with complex
then section of a request. This way, the specified assertions will be JSON structures.
applied to the response. For example:
Rest Assured Interview Question Banks 5 Rest Assured Interview Question Banks 6
17. How do you handle SSL in REST Assured? 22. What is BDD in REST Assured?
REST Assured handles SSL by default. However, if you need to work with BDD in REST Assured refers to writing tests in a human-readable
self-signed certificates or specific truststores, you can configure SSL language that closely resembles natural language. It involves using
using the trustStore() and relaxedHTTPSValidation() methods within the methods like given() , when() , and then() to describe the behavior of the
given() section. API under test, making tests more expressive.
18. What is "log()" in REST Assured? 23. How can you effectively manage dynamic values, such as timestamps, when
validating responses using REST Assured?
The log() method in REST Assured is used for logging request and
response details. It can help diagnose issues and provide insight into the To handle dynamic values like timestamps during response validation with
interactions with the API. REST Assured, you can utilize a range of JSONPath functions, such as
" matches() " or " contains() ".
19. How do you set headers in REST Assured?
You can use the header() method within the given() section to set headers given().
for a request. For example: get("https://api.example.com/resource").
then().
javaCopy code assertThat().
given() body("timestamp", matchesRegex("\\d{4}-\\d{2}-\\d{2}T\\d{
.header("Authorization", "Bearer token")
.header("Content-Type", "application/json")
.when()
.get("https://api.example.com/resource"); 24. How can you validate a numeric response using REST Assured?
To validate a numeric response value using REST Assured, you can employ
Hamcrest matchers. For instance, you can utilize "equalTo()" to ascertain if a
response value aligns with an anticipated numeric value.
21. What is the difference between "given()", "when()", and "then()" in REST
import static org.hamcrest.Matchers.equalTo;
Assured?
In REST Assured, given() is used to set up preconditions for a request, public void validateNumericResponse() {
when()is used to execute the request, and then() is used to perform
assertions on the response. These methods help structure tests in a clear RestAssured.given()
and expressive manner. .when()
.get("https://api.example.com/numeric")
.then()
Rest Assured Interview Question Banks 7 Rest Assured Interview Question Banks 8
.assertThat() // Retrieving status code
.statusCode(200) int statusCode = response.getStatusCode();
.body("numericField", equalTo(42)); // Validate that the resp
} // Printing status code
System.out.println("Status Code: " + statusCode);
}
25. How does REST Assured handle Unicode characters when testing APIs?
REST Assured automatically manages Unicode characters in both requests
27. Describe how to extract response time information from a response using
and responses. Typically, no additional configuration or handling is
REST Assured?
necessary…..
You can use the "time()" method on a Response object to extract the
response time in milliseconds. For example:
public static void main(String[] args) {
// Setting up base URI // Send a request and capture the response
RestAssured.baseURI = "https://api.example.com"; Response response = RestAssured.get("https://api.example.com/
// Sending GET request // Extract the response time in milliseconds
Response response = RestAssured.get("/endpoint"); long responseTime = response.time();
// Retrieving response body
String responseBody = response.getBody().asString();
// Printing response body
System.out.println("Response Body: " + responseBody);
}
26. Describe how to perform HEAD requests using REST Assured?
public static void main(String[] args) {
// Setting up base URI
RestAssured.baseURI = "<https://api.example.com>";
// Sending HEAD request
Response response = RestAssured.head("/resource");
Rest Assured Interview Question Banks 9 Rest Assured Interview Question Banks 10