Sunday 24 November 2019

Rest Assured: DELETE Method example


Using ‘delete’ method, you can issue a DELETE request.

Example
Response response = given().header("Accept", "application/json").header("Content-Type", "application/json")
.when().delete("api/v1/employees/1").then().assertThat().statusCode(200).and()
.contentType(ContentType.JSON).extract().response();

TestApp.java
package com.sample.app;

import static io.restassured.RestAssured.given;

import org.junit.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

public class TestApp {

 @Test
 public void testApp() {
  RestAssured.baseURI = "http://localhost:8080";

  Response response = given().header("Accept", "application/json").header("Content-Type", "application/json")
    .when().delete("api/v1/employees/1").then().assertThat().statusCode(200).and()
    .contentType(ContentType.JSON).extract().response();

  JsonPath jsonPath = new JsonPath(response.asString());

  int empId = jsonPath.get("id");
  String firstName = jsonPath.get("firstName");
  String lastName = jsonPath.get("lastName");

  System.out.println("empId : " + empId);
  System.out.println("firstName : " + firstName);
  System.out.println("lastName : " + lastName);

 }

}


Run TestApp.java as junit test, you will see below messages in console.
empId : 1
firstName : Deepak
lastName : Moud

Run TestApp.java again, you will see below error.
java.lang.AssertionError: 1 expectation failed.

Expected status code <200> but was <404>.


Previous                                                    Next                                                    Home

No comments:

Post a Comment