Rest Assured Interview Questions Bank
Rest Assured Interview Questions Bank
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
Maven, you'd add the following dependency.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
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 provides various methods for response validation. You can
use the then on a Response object to apply assertions on status codes,
headers, response body, etc. For example:
response.then().statusCode(200).contentType("applicatio
n/json");
To send a POST request using REST Assured, you can use the given() method
to set request specifications and then use the post() method to send the
request. For example:
given()
.contentType("application/json")
.body(requestBody)
.when()
.post("https://api.example.com/resource")
.then()
.statusCode(201);
8. How do you send PUT and DELETE requests with REST Assured?
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")
.when()
.get("https://api.example.com/resource")
.then()
.statusCode(200);
You can use the queryParam() method within the given() section to add
query parameters to the request. For validation, you can use the then()
section to assert the presence or value of the query parameter in the
response. For example:
javaCopy code
given()
.queryParam("key", "value")
.when()
.get("https://api.example.com/resource")
.then()
.statusCode(200)
.body("queryParamkey", equalTo("expectedValue"));
given()
.when()
.get("https://api.example.com/resource")
.then()
.spec(responseSpec);
REST Assured allows you to set timeout values using the given() section. For
example, you can use the timeouts() method to define the connection and
request timeouts. For instance:
javaCopy code
given()
.timeouts().connectTimeout(5000).readTimeout(5000)
.when()
.get("https://api.example.com/resource");
REST Assured handles SSL by default. However, if you need to work with
self-signed certificates or specific truststores, you can configure SSL
using the trustStore() and relaxedHTTPSValidation() methods within the
given() section.
The log() method in REST Assured is used for logging request and
response details. It can help diagnose issues and provide insight into the
interactions with the API.
You can use the header() method within the given() section to set headers
for a request. For example:
javaCopy code
given()
.header("Authorization", "Bearer token")
.header("Content-Type", "application/json")
.when()
.get("https://api.example.com/resource");
21. What is the difference between "given()", "when()", and "then()" in REST
Assured?
23. How can you effectively manage dynamic values, such as timestamps, when
validating responses using REST Assured?
given().
get("https://api.example.com/resource").
then().
assertThat().
body("timestamp", matchesRegex("\\d{4}-\\d{2}-\\d{2}T\\d{
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.
RestAssured.given()
.when()
.get("https://api.example.com/numeric")
.then()
25. How does REST Assured handle Unicode characters when testing APIs?
27. Describe how to extract response time information from a response using
REST Assured?
You can use the "time()" method on a Response object to extract the
response time in milliseconds. For example: