Rest Assured Questions and Answers
Rest Assured Questions and Answers
Rest Assured Questions and Answers
Chaining in Rest Assured refers to the technique of linking multiple method calls together
in a single statement to improve readability and conciseness. Each method returns an
instance of a class that allows further method calls.
given()
.header("Content-Type", "application/json")
.body("{ \"name\": \"John\" }")
.when()
.post("/users")
.then()
.statusCode(201)
.body("name", equalTo("John"));
In the above example, given(), when(), and then() methods are chained together. The given()
method sets up the request, when() sends it, and then() validates the response.
Assertions are used to verify that the response received from an API matches the
expected outcome. They ensure that the API behaves correctly and returns the correct
data.
given()
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("John"));
Here, the statusCode(200) assertion checks if the response status code is 200, and
body("name", equalTo("John")) verifies that the name in the response body is "John".
Example:
If the server is not available, you typically receive a 503 Service Unavailable status code. This
indicates that the server is temporarily unable to handle the request.
6. What is the difference between String, StringBuilder, and StringBuffer? Which should be
used when?
o String: Immutable sequence of characters. Once created, the value cannot be
changed.
o StringBuilder: Mutable sequence of characters, not thread-safe. Suitable for
single-threaded environments where frequent modifications are needed.
o StringBuffer: Mutable sequence of characters, thread-safe. Suitable for multi-
threaded environments where frequent modifications are needed.
7. If there are a lot of transactions in the database, then which one of the above should be
used?
Custom exceptions are created by extending the Exception class or any of its subclasses.
You can throw and catch this exception like any other exception.
if (conditionFails) {
throw new CustomException("Custom error message");
}
9. If the design pattern is singleton and you are using a parallel run, will it execute in parallel
or not?
Singleton pattern ensures that only one instance of a class is created. However, if you are
using a singleton in a multi-threaded environment, you need to ensure thread safety
using synchronization.
private Singleton() {}
Serialization is the process of converting a Java object into a JSON or XML representation for
sending it in the API request.
given()
.contentType("application/json")
.body(user)
.when()
.post("/users")
.then()
.statusCode(201);
• As a JSON string
• Using a HashMap
• Using a POJO (Plain Old Java Object)
// JSON String
String jsonString = "{ \"name\": \"John\", \"age\": 30 }";
// HashMap
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("name", "John");
jsonMap.put("age", 30);
// POJO
class User {
private String name;
private int age;
Query parameters are key-value pairs appended to the URL to pass additional data to the server.
given()
.queryParam("search", "John")
.when()
.get("/users");
• PUT: Replaces the entire resource with the new data provided.
• PATCH: Partially updates the resource with the new data provided.
// PUT request
given()
.contentType("application/json")
.body(new User("John", 31))
.when()
.put("/users/1")
.then()
.statusCode(200);
// PATCH request
given()
.contentType("application/json")
.body("{ \"age\": 31 }")
.when()
.patch("/users/1")
.then()
.statusCode(200);
14. How do you handle authentication and authorization in API automation testing?
// Basic Authentication
given()
.auth()
.basic("username", "password")
.when()
.get("/protected");
// OAuth2
given()
.auth()
.oauth2("accessToken")
.when()
.get("/protected");
Authentication and authorization are handled by setting the appropriate headers or tokens in
the request.
15. What is JSON? How do you parse and validate JSON responses in API automation testing?
JSON (JavaScript Object Notation) is a lightweight data interchange format. In Rest Assured, you
can parse and validate JSON responses using the body() method and JSONPath expressions.
given()
.when()
.get("/users/1")
.then()
.body("name", equalTo("John"))
.body("age", equalTo(30));
16. How do you handle dynamic data in API responses during automation?
Extract dynamic data from the response and use it in subsequent requests.
Endpoint testing involves testing individual API endpoints to ensure they work as expected. This
includes checking the status code, response body, headers, and other response details.
given()
.when()
.get("/endpoint")
.then()
.statusCode(200)
.body("key", equalTo("value"));
18. How do you handle error responses and status codes in API automation testing?
Validate error responses and status codes to ensure the API handles errors correctly.
given()
.when()
.get("/nonexistent")
.then()
.statusCode(404)
.body("error", equalTo("Not Found"));
19. What are some common challenges faced in API automation testing, and how do you
overcome them?
given()
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("John"));
Status codes indicate the result of the API request. For example:
given()
.queryParam("key", "value")
.when()
.get("/endpoint");
23. Explain how to validate JSON responses using Rest Assured.
given()
.when()
.get("/users/1")
.then()
.body("name", equalTo("John"))
.body("age", equalTo(30));
// Basic Authentication
given()
.auth()
.basic("username", "password")
.when()
.get("/protected");
// OAuth2
given()
.auth()
.oauth2("accessToken")
.when()
.get("/protected");
26. How do you perform POST and PUT requests using Rest Assured?
// POST request
given()
.contentType("application/json")
.body(new User("John", 30))
.when()
.post("/users")
.then()
.statusCode(201);
// PUT request
given()
.contentType("application/json")
.body(new User("John", 31))
.when()
.put("/users/1")
.then()
.statusCode(200);
given()
.when()
.get("/users/1")
.then()
.spec(responseSpec);
28. How can you extract data from a response using Rest Assured?
30. What are the benefits of using Rest Assured for API testing?