API testing has become an essential skill for modern QA Automation Engineers. With applications becoming more distributed through microservices and REST APIs, companies expect automation engineers to have strong knowledge of API automation frameworks. In this article we will cover REST Assured Interview Questions and Answers.
What is Rest Assured?
Rest Assured is an open-source Java library used for testing RESTful APIs.
It provides a simple DSL (Domain Specific Language) to automate HTTP requests and validate responses.
Rest Assured supports:
- GET
- POST
- PUT
- PATCH
- DELETE
- Authentication
- Headers
- Cookies
- Request body validation
- Response validation
- JSON/XML parsing
- Serialization and Deserialization
Example:
given() .baseUri("https://api.example.com").when() .get("/users").then() .statusCode(200);
How do you configure Rest Assured in a Maven project?
Add the dependency in pom.xml.
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.5.0</version> <scope>test</scope></dependency>
For Hamcrest assertions:
<dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest</artifactId> <version>2.2</version></dependency>
For JSON Schema validation:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>json-schema-validator</artifactId> <version>5.5.0</version></dependency>
How do you automate a GET request using Rest Assured?
Example API:
GET /users/1
Automation code:
@Testpublic void getUser(){given() .baseUri("https://reqres.in").when() .get("/api/users/1").then() .statusCode(200);}
How do you validate response data from GET API?
Using Hamcrest:
given().when() .get("/api/users/1").then() .statusCode(200) .body("data.first_name", equalTo("George"));
How do you send POST request in Rest Assured?
POST request creates a new resource.
Example JSON:
{ "name":"John", "job":"QA Engineer"}
Rest Assured:
@Testpublic void createUser(){String requestBody ="""{"name":"John","job":"QA Engineer"}""";given() .contentType(ContentType.JSON) .body(requestBody).when() .post("/api/users").then() .statusCode(201);}
Difference between PUT and PATCH?
| PUT | PATCH |
|---|---|
| Complete resource update | Partial resource update |
| Sends entire object | Sends only changed fields |
| Usually idempotent | May or may not be idempotent |
Example PUT:
given().body(userObject).when().put("/users/10").then().statusCode(200);
How do you automate PATCH request?
Example:
given().contentType(ContentType.JSON).body("""{"name":"Updated Name"}""").when().patch("/users/10").then().statusCode(200);
How do you automate DELETE API?
given().when().delete("/users/10").then().statusCode(204);
What is Request Specification?
Request Specification is used to avoid duplicate code by storing common request configurations.
Common configurations:
- Base URL
- Headers
- Authentication
- Content Type
- Cookies
Example:
RequestSpecification requestSpec;@BeforeClasspublic void setup(){requestSpec =given().baseUri("https://api.example.com").contentType(ContentType.JSON);}
Usage:
given().spec(requestSpec).when().get("/users").then().statusCode(200);
What is Response Specification?
Response Specification stores common response validations.
Example:
ResponseSpecification responseSpec =expect().statusCode(200).contentType(ContentType.JSON);
Usage:
given().when().get("/users").then().spec(responseSpec);
What are Hamcrest Matchers?
Hamcrest provides readable assertions for validating API responses.
Common matchers:
- equalTo()
- containsString()
- hasItem()
- greaterThan()
- lessThan()
Example:
given().when().get("/employees").then().body("employees.size()", greaterThan(0)).body("employees.name", hasItem("John"));
Why do we use JSON Schema Validation?
JSON Schema validation verifies that API response structure follows the expected contract.
Example schema:
user-schema.json
{"type":"object","properties":{"id":{"type":"integer"},"name":{"type":"string"}}}
Validation:
given().when().get("/users/1").then().body(matchesJsonSchemaInClasspath("user-schema.json"));
What is Serialization?
Serialization converts Java objects into JSON format.
Example:
Java Object:
User user = new User();user.setName("John");user.setJob("Tester");
Convert into JSON:
given().body(user).post("/users");
Rest Assured automatically serializes the object.
What is Deserialization?
Deserialization converts JSON response into Java objects.
Example Response:
{"id":1,"name":"John"}
Java:
User user =response.as(User.class);System.out.println(user.getName());
Why do we use POJO classes?
POJO classes provide:
- Maintainable request objects
- Type safety
- Reusable models
- Cleaner automation framework
Example:
public class User {private String name;private String job;public String getName(){return name;}public void setName(String name){this.name=name;}}
Usage:
User user=new User();user.setName("Alex");given().body(user).post("/users");
What is ObjectMapper?
ObjectMapper is a Jackson library class used for converting Java objects to JSON and JSON to Java objects.
Convert Object to JSON
ObjectMapper mapper=new ObjectMapper();String json =mapper.writeValueAsString(user);System.out.println(json);
Convert JSON to Object
User user =mapper.readValue(json, User.class);
How do you enable logging in Rest Assured?
Request logging:
given().log().all().when().get("/users");
Response logging:
then().log().all();
Log only when validation fails
given().when().get("/users").then().log().ifValidationFails().statusCode(200);
What are Filters in Rest Assured?
Filters allow modification or monitoring of requests and responses.
Common uses:
- Custom logging
- Reporting
- Authentication handling
- Request/response tracking
Example:
given().filter(new RequestLoggingFilter()).filter(new ResponseLoggingFilter()).when().get("/users");
Custom Logging Filter Example
public class CustomFilter implements Filter {public Response filter(FilterableRequestSpecification request,FilterableResponseSpecification response,FilterContext context){System.out.println(request.getURI());return context.next(request,response);}}
Usage:
given().filter(new CustomFilter()).get("/users");
Status code is 200 but response body is empty. What will you validate?
I will validate:
- Response body is not null
- Content-Length header
- Mandatory JSON fields
- API contract
Example:
.then().body(not(emptyString())).header("Content-Type",containsString("application/json"));
How do you create reusable API automation framework?
Typical framework structure:
src/test/java├── base│ └── BaseTest.java│├── api│ └── UserAPI.java│├── models│ └── User.java│├── utils│ └── ObjectMapperUtil.java│├── specifications│ ├── RequestSpec.java│ └── ResponseSpec.java│└── tests └── UserTest.java
Best Practices for Rest Assured Automation
1. Use RequestSpecification
Avoid duplicate configurations.
2. Use POJO Classes
Improve maintainability.
3. Validate Schema
Catch API contract failures.
4. Separate API Layer
Keep tests clean.
5. Add Logging and Reporting
Debug failures quickly.
6. Use Environment Configuration
Manage URLs and credentials externally.
Conclusion
Rest Assured is one of the most important skills for API automation engineers. For senior QA automation interviews, understanding only HTTP methods is not enough.
Interviewers expect knowledge of:
- REST API automation using Java
- Request and Response Specifications
- Hamcrest assertions
- JSON Schema validation
- Serialization and Deserialization
- ObjectMapper
- POJO Classes
- Logging
- Filters
- Framework design
Mastering these Rest Assured interview questions will help you confidently answer API automation questions in SDET and Senior QA Automation Engineer interviews.
Related Links
Discover more from Rotebit
Subscribe to get the latest posts sent to your email.
