Top 100 Interview Questions with Answer on REST Assured

 Top 100 Interview Questions with Answer on REST Assured


1. What is REST Assured?

   - REST Assured is a Java library that provides a domain-specific language (DSL) for testing RESTful web services. It simplifies the process of writing automated tests for RESTful APIs.


2. How do you add REST Assured to your Maven project?

   - To add REST Assured to a Maven project, you need to include the following dependency in your project's pom.xml file:

     ```xml

     <dependency>

         <groupId>io.rest-assured</groupId>

         <artifactId>rest-assured</artifactId>

         <version>4.3.3</version>

         <scope>test</scope>

     </dependency>

     ```


3. How do you send a GET request using REST Assured?

   - You can send a GET request using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     Response response = get("https://api.example.com/users/1");

     ```


4. How do you verify the status code of a response using REST Assured?

   - You can verify the status code of a response using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     get("https://api.example.com/users/1")

         .then()

         .statusCode(200);

     ```


5. How do you extract response data using REST Assured?

   - You can extract response data using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     Response response = get("https://api.example.com/users/1");

     String name = response.jsonPath().get("name");

     ```


6. How do you send a POST request using REST Assured?

   - You can send a POST request using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     given()

         .contentType("application/json")

         .body("{ \"name\": \"John\", \"email\": \"john@example.com\" }")

     .when()

         .post("https://api.example.com/users")

     .then()

         .statusCode(201);

     ```


7. How do you pass query parameters in a request using REST Assured?

   - You can pass query parameters in a request using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     given()

         .queryParam("name", "John")

         .queryParam("age", 30)

     .when()

         .get("https://api.example.com/users")

     .then()

         .statusCode(200);

     ```


8. How do you send a PUT request using REST Assured?

   - You can send a PUT request using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     given()

         .contentType("application/json")

         .body("{ \"name\": \"John Doe\" }")

     .when()

         .put("https://api.example.com/users/1")

     .then()

         .statusCode(200);

     ```


9. How do you send a DELETE request using REST Assured?

   - You can send a DELETE request using REST Assured as follows:

     ```java

     import static io.restassured.RestAssured.*;

     import static org.hamcrest.Matchers.*;


     when()

         .


delete("https://api.example.com/users/1")

     .then()

         .statusCode(204);

     ```


10. How do you perform authentication using REST Assured?

    - You can perform authentication using REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .auth()

          .basic("username", "password")

      .when()

          .get("https://api.example.com/users")

      .then()

          .statusCode(200);

      ```


11. How do you set headers in a request using REST Assured?

    - You can set headers in a request using REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .header("Authorization", "Bearer token")

          .header("Content-Type", "application/json")

      .when()

          .get("https://api.example.com/users")

      .then()

          .statusCode(200);

      ```


12. How do you perform response validation using REST Assured?

    - You can perform response validation using REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      get("https://api.example.com/users")

          .then()

          .statusCode(200)

          .body("name", hasItem("John"))

          .body("age", greaterThan(20));

      ```


13. How do you handle JSON responses in REST Assured?

    - REST Assured automatically handles JSON responses. You can extract values from JSON using the `.jsonPath()` method, and perform assertions on JSON using Hamcrest matchers.


14. How do you handle XML responses in REST Assured?

    - REST Assured can handle XML responses. You can extract values from XML using the `.xmlPath()` method, and perform assertions on XML using Hamcrest matchers.


15. How do you handle cookies in REST Assured?

    - You can handle cookies in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .cookie("sessionId", "12345")

      .when()

          .get("https://api.example.com/users")

      .then()

          .statusCode(200);

      ```


16. How do you handle timeouts in REST Assured?

    - You can set timeouts in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .urlEncodingEnabled(true)

          .urlEncodingTimeout(5000)

      .when()

          .get("https://api.example.com/users")

      .then()

          .statusCode(200);

      ```


17. How do you handle SSL certificates in REST Assured?

    - You can handle SSL certificates in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      useRelaxedHTTPSValidation();

      ```


18. How do you perform parameterized tests with REST Assured?

    - You can perform parameterized tests with REST Assured using testing frameworks like TestNG or JUnit. You can pass different test data to your test methods and execute them multiple times.


19. How do you handle authentication tokens in REST Assured?

    - You can handle authentication tokens in REST Assured by including them in the request headers or query parameters, depending on the API requirements.


20. How do you handle dynamic responses in REST Assured?

    - REST Assured provides several methods to handle dynamic


 responses, such as using path parameters, extracting values using JSON or XML path expressions, and using conditional assertions.


21. How do you handle API versioning in REST Assured?

    - API versioning can be handled in REST Assured by including the version information in the API endpoint URLs or by using custom request headers to specify the API version.


22. How do you handle multipart/form-data requests in REST Assured?

    - You can handle multipart/form-data requests in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .multiPart("file", new File("path/to/file.txt"))

      .when()

          .post("https://api.example.com/upload")

      .then()

          .statusCode(200);

      ```


23. How do you handle cookies in REST Assured?

    - You can handle cookies in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .cookie("sessionId", "12345")

      .when()

          .get("https://api.example.com/users")

      .then()

          .statusCode(200);

      ```


24. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .urlEncodingEnabled(true)

          .urlEncodingTimeout(5000)

      .when()

          .get("https://api.example.com/users")

      .then()

          .statusCode(200);

      ```


25. How do you handle SSL certificates in REST Assured?

    - You can handle SSL certificates in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      useRelaxedHTTPSValidation();

      ```


26. How do you perform parameterized tests with REST Assured?

    - You can perform parameterized tests with REST Assured using testing frameworks like TestNG or JUnit. You can pass different test data to your test methods and execute them multiple times.


27. How do you handle authentication tokens in REST Assured?

    - You can handle authentication tokens in REST Assured by including them in the request headers or query parameters, depending on the API requirements.


28. How do you handle dynamic responses in REST Assured?

    - REST Assured provides several methods to handle dynamic responses, such as using path parameters, extracting values using JSON or XML path expressions, and using conditional assertions.


29. How do you handle API versioning in REST Assured?

    - API versioning can be handled in REST Assured by including the version information in the API endpoint URLs or by using custom request headers to specify the API version.


30. How do you handle multipart/form-data requests in REST Assured?

    - You can handle multipart/form-data requests in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .multiPart("file", new File("path/to/file.txt"))

      .when()

          .post("https://api.example.com/upload")

      .then()

          .statusCode(200);

      ```


31. How do you handle redirects in REST Assured?

    - By default, REST Assured automatically follows redirects. You can disable this behavior by setting the `followRedirects` property to `false` in the configuration.


32. How do you handle request and response logging in REST Assured?

    - You can enable request and response logging in REST


 Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .log().all()

      .when()

          .get("https://api.example.com/users")

      .then()

          .log().all()

          .statusCode(200);

      ```


33. How do you handle authentication challenges in REST Assured?

    - REST Assured automatically handles authentication challenges like Basic Authentication or OAuth 2.0 by providing the necessary credentials or tokens in the request.


34. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


35. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


36. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


37. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


38. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


39. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


40. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


41. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


42. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


43. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


44. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


45. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


46. How do you handle request and response logging in REST Assured?

    - You can enable request and response logging in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .log().all()

      .when()

          .get("https://api.example.com/users")

      .then()

          .log().all()

          .statusCode(200);

      ```


47. How do you handle authentication challenges in REST Assured?

    - REST Assured automatically handles


 authentication challenges like Basic Authentication or OAuth 2.0 by providing the necessary credentials or tokens in the request.


48. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


49. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


50. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


51. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


52. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


53. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


54. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


55. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


56. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


57. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


58. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


59. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


60. How do you handle request and response logging in REST Assured?

    - You can enable request and response logging in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .log().all()

      .when()

          .get("https://api.example.com/users")

      .then()

          .log().all()

          .statusCode(200);

      ```


61. How do you handle authentication challenges in REST Assured?

    - REST Assured automatically handles authentication challenges like Basic Authentication or OAuth 2.0 by providing the necessary credentials or tokens in the request.


62. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


63. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by


 using JSON path expressions to extract values from the objects or perform assertions on the object properties.


64. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


65. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


66. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


67. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


68. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


69. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


70. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


71. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


72. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


73. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


74. How do you handle request and response logging in REST Assured?

    - You can enable request and response logging in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .log().all()

      .when()

          .get("https://api.example.com/users")

      .then()

          .log().all()

          .statusCode(200);

      ```


75. How do you handle authentication challenges in REST Assured?

    - REST Assured automatically handles authentication challenges like Basic Authentication or OAuth 2.0 by providing the necessary credentials or tokens in the request.


76. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


77. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


78. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


79. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Ass


ured by using the `.header()` method with the header name to extract the value from the response.


80. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


81. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


82. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


83. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


84. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


85. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


86. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


87. How do you handle timeouts in REST Assured?

    - You can handle timeouts in REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


88. How do you handle request and response logging in REST Assured?

    - You can enable request and response logging in REST Assured as follows:

      ```java

      import static io.restassured.RestAssured.*;

      import static org.hamcrest.Matchers.*;


      given()

          .log().all()

      .when()

          .get("https://api.example.com/users")

      .then()

          .log().all()

          .statusCode(200);

      ```


89. How do you handle authentication challenges in REST Assured?

    - REST Assured automatically handles authentication challenges like Basic Authentication or OAuth 2.0 by providing the necessary credentials or tokens in the request.


90. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


91. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


92. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


93. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


94. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.


95. How do you handle timeouts in REST Assured?

    - You can handle timeouts in


 REST Assured by using the `.timeout()` method to specify the desired timeout value in milliseconds for the request.


96. How do you handle JSON arrays in REST Assured?

    - You can handle JSON arrays in REST Assured by using JSON path expressions to extract values from the array or perform assertions on the array elements.


97. How do you handle JSON objects in REST Assured?

    - You can handle JSON objects in REST Assured by using JSON path expressions to extract values from the objects or perform assertions on the object properties.


98. How do you handle XML namespaces in REST Assured?

    - You can handle XML namespaces in REST Assured by using the `.using()` method with the `XmlPathConfig` parameter to specify the desired XML namespace mappings.


99. How do you handle custom response headers in REST Assured?

    - You can handle custom response headers in REST Assured by using the `.header()` method with the header name to extract the value from the response.


100. How do you handle error responses in REST Assured?

    - You can handle error responses in REST Assured by using the `.statusCode()` method to verify the expected status code or by extracting error messages from the response body and asserting on them.

Post a Comment

0 Comments