May 28, 2023

REST Assured

Great job on starting a new lesson! After reading this lesson, click Next 👉 button at bottom right to continue to the next lesson.

REST Assured is a popular Java library used for testing RESTful APIs. It provides a simple and intuitive syntax that allows you to write API tests in a readable and concise manner.

With REST Assured, you send HTTP requests, specify request headers, query parameters, and request bodies, validate the responses received from the API, and perform assertions. REST Assured supports various authentication methods, JSON/XML parsing, and assertion libraries.

Examples of Rest Assured

Sending a GET request to retrieve user information

given()
  .baseUri("https://api.example.com")
  .basePath("/users")
  .when()
  .get("/123")
  .then()
  .assertThat()
  .statusCode(200)
  .body("name", equalTo("John Doe"));

Creating a new user using a POST request

given()
  .baseUri("https://api.example.com")
  .basePath("/users")
  .contentType("application/json")
  .body("{ \"name\": \"Jane Smith\", \"email\": \"jane@example.com\" }")
  .when()
  .post()
  .then()
  .assertThat()
  .statusCode(201);

Tips for REST Assured

  • Use the given-when-then structure to organize your test steps and assertions.
  • Use REST Assured's built-in assertion methods to validate response status codes, headers, and response body content.
  • You can take REST Assured's support for authentication methods like Basic Auth, OAuth, and API keys.

FAQ (interview questions and answers)

  1. Can REST Assured handle JSON and XML responses?
    Yes, Rest Assured has built-in support for parsing and validating both JSON and XML responses.
  2. Is REST Assured only for Java projects?
    No, REST Assured is primarily a Java library, but it can be used in conjunction with other programming languages and frameworks through API calls or HTTP requests.
  3. Does REST Assured support parallel test execution?
    Yes, REST Assured allows you to run your API tests concurrently to reduce test execution time.
  4. Can REST Assured integrate with test frameworks like JUnit or TestNG?
    Yes, REST Assured integrates with JUnit and TestNG, which can include API tests into your existing test suites.
Remember to just comment if you have any doubts or queries.
REST Assured short tutorial

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.