Validating GET, POST, PUT and DELETE responses in RestAssured

Validating GET, POST, PUT and DELETE responses in RestAssured

Rest Assured is a Java library used for testing RESTful APIs. It provides an easy-to-use API for creating and executing HTTP requests, and then validating the response returned by the server.

Validating response in Rest Assured:

  1. Send a Request: First, you need to send a request to the API endpoint using Rest Assured. You can use the given() method to specify the base URL and the request path, and then use the when() method to send the request and specify the HTTP method and any request parameters.
  2. Receive a Response: Once the request is sent, the API will return a response. You can capture the response using the then() method.
  3. Validate the Response: Rest Assured provides several methods for validating the API response, such as:

a. Status Code: You can use the assertThat() method to verify the status code of the response. For example, assertThat().statusCode(200).

b. Response Body: You can use the assertThat() method to verify the response body of the response. For example, assertThat().body("name", equalTo("John Doe")).

c. Response Headers: You can use the assertThat() method to verify the headers of the response. For example, assertThat().header("Content-Type", equalTo("application/json")).


Validating GET response

@Test

public void testGetApi() {

Response response = given()

.when()

.get("/users/12345");

response.then().statusCode(200);

response.then().body("name", equalTo("John Doe"));

response.then().body("age", equalTo(30));

}

In this example, we're validating the status code, name, and age of the response received for the GET request.

Validating POST response:

@Test

public void testPostApi() {

Response response = given()

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

.when()

.post("/users");

response.then().statusCode(201);

response.then().body("name", equalTo("John Doe"));

response.then().body("age", equalTo(30));

}

In this example, we're validating the status code, name, and age of the response received for the POST request.

Validating PUT request

@Test

public void testPutApi() {

Response response = given()

.body("{ \"name\": \"Jane Doe\", \"age\": 35 }")

.when()

.put("/users/12345");

response.then().statusCode(200);

response.then().body("name", equalTo("Jane Doe"));

response.then().body("age", equalTo(35));

}

In this example, we're validating the status code, name, and age of the response received for the PUT request.

Validating DELETE response

@Test

public void testDeleteApi() {

Response response = given()

.when()

.delete("/users/12345");

response.then().statusCode(204);

}

In this example, we're validating the status code of the response received for the DELETE request. Since the DELETE request typically does not return a response body, we don't need to validate the response body. Instead, we only need to validate the status code to ensure that the resource was deleted successfully.

要查看或添加评论,请登录

Mohith Nayak的更多文章

  • Accessibility Automation Testing - Axe Core

    Accessibility Automation Testing - Axe Core

    In this article lets see how we can automate our Accessibility test cases using axe-core library. Lets me first define…

  • Accessibility Testing

    Accessibility Testing

    Accessibility testing is a type of software testing that ensures that a product or service is accessible to people with…

  • Smoke Testing Vs Sanity Testing

    Smoke Testing Vs Sanity Testing

    Smoke Testing and Sanity Testing are both types of software testing performed during the software development…

  • API Testing Interview Questions - Part 1

    API Testing Interview Questions - Part 1

    What are some common HTTP methods used in API testing? HTTP methods used in API testing include GET, POST, PUT, DELETE,…

    5 条评论
  • Exploratory Testing

    Exploratory Testing

    Exploratory testing is a software testing approach that emphasizes the tester's freedom and creativity in identifying…

  • Levels of Software Testing

    Levels of Software Testing

    Levels of Testing There are typically four levels of software testing, which build on each other to ensure the quality…

  • Functional Testing

    Functional Testing

    Functional testing : It is a type of software testing that checks whether an application or system meets its functional…

社区洞察

其他会员也浏览了