Table of Contents
What we have learned so far?
https://www.onlyfullstack.com/what-is-rest-assured-how-to-setup-rest-assured/
Part 2 – Sample Rest API To Test With Rest Assured
https://www.onlyfullstack.com/sample-rest-api-to-test-with-rest-assured/
Part 3 – How to make a GET Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-get-request-in-rest-assured/
Part 4 – How to make a POST Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-post-request-in-rest-assured/
Part 5 – How to make a PUT Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-put-request-in-rest-assured/
What is HTTP DELETE Request?
The HTTP DELETE method is used to delete a resource from the server. In our employee rest api you can hit the Delete Employee request to delete the details of the employees.
Delete Employee –
HTTP Method – DELETE
URL – http://localhost:8088/employees/1
URL Parameters – employee id which we need to update. we have passed this information as a Path Param after employees. In above url we have passed the employee id as 2.
Body – No body required
Response – No Response
How to send DELETE Request in Rest Assured?
Lets write code to automate this delete request and check the output –
Rest assured delete api automation without BDD approach
@Test public void deleteMethodWithoutBDDApproach() { String requestEmployeeId = "2"; RequestSpecification request = RestAssured.given(); request.baseUri("http://localhost:8088"); request.pathParam("employee_id", requestEmployeeId); Response response = request.delete("employees/{employee_id}"); // Let's print response body. String responseString = response.asString(); System.out.println("Response Details : " + responseString); /* * To perform validation on response like status code or value, we need to get * ValidatableResponse type of response using then() method of Response * interface. ValidatableResponse is also an interface. */ ValidatableResponse validatableResponse = response.then(); // It will check if status code is 200 validatableResponse.statusCode(200); }
As we have seen in the http delete request method with Rest Assured we have explored all the lines shown above. The only difference we are doing
1. Set URI
First we need to set the base uri of our api. uri is the base url without the resource. means in our case the complete url is http://localhost:8088/employees/1 and here the uri is http://localhost:8088/ and the resource name is employee. We also need to pass the employee id as a path parameter so we would be calling the pathParam method and passing the employee id which needs to be updated into database.
RequestSpecification request = RestAssured.given() .pathParam("employee_id", requestEmployeeId); request.baseUri(“http://localhost:8088/”);
2. Call http delete request method
RestAssured has a static overloaded method named delete(String resourceName) which returns a reference of Response interface. In fact return type of all http methods in RestAssured class is of type Response. This response contains every details returned by hitting request i.e. response body, response headers, status code, status lines, cookies etc. Here we have captured the response and printed on console.
request.contentType(ContentType.JSON); Response response = request.delete("/employees/{employee_id}");
3. Validate the response
To validate response like status code or value , we need to get reference of type ValidatableResponse. ValidatableResponse is an interface. Response interface has a method named “then()” which returns ValidatableResponse. In fact there is an interface called “Validatable” which has “then()” method. Response interface extends
Once we get ValidatableResponse reference, we can do many assertions. In this post, we will verify status code.
In below code we are only validating the status code as delete api does not return any response in the body.
/* * To perform validation on response like status code or value, we need to get * ValidatableResponse type of response using then() method of Response * interface. ValidatableResponse is also an interface. */ ValidatableResponse validatableResponse = response.then(); // It will check if status code is 201 validatableResponse.statusCode(200);
Rest assured http delete request api automation with BDD approach
BDD is an agile software development process (Not a Software Testing process) which defines expectation from an application to behave from intended user prospective. Don’t worry about the BDD we will discuss in the next blog.
Each scenario is written in form of
- Given (Pre-conditions/ Context),
- When (Action/Event Performed) and
- Then (Result/Outcomes) format.
@Test public void deleteMethodWithBDDApproach() { String requestEmployeeId = "1"; RestAssured.given() //precondition is to give the base uri .baseUri("http://localhost:8088/") .pathParam("employee_id", requestEmployeeId) .when() //actual action is to perform the delete request .delete("employees/{employee_id}") .then() // will return the ValidatadleResponse from the Response object .statusCode(200); }
Below is the complete program for HTTP post method with rest assured
package onlyfullstack.httpMethods; import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; import org.testng.annotations.Test; public class DeleteHttpMethodExample { @Test public void deleteMethodWithoutBDDApproach() { String requestEmployeeId = "2"; RequestSpecification request = RestAssured.given(); request.baseUri("http://localhost:8088"); request.pathParam("employee_id", requestEmployeeId); Response response = request.delete("employees/{employee_id}"); // Let's print response body. String responseString = response.asString(); System.out.println("Response Details : " + responseString); /* * To perform validation on response like status code or value, we need to get * ValidatableResponse type of response using then() method of Response * interface. ValidatableResponse is also an interface. */ ValidatableResponse validatableResponse = response.then(); // It will check if status code is 200 validatableResponse.statusCode(200); } @Test public void deleteMethodWithBDDApproach() { String requestEmployeeId = "1"; RestAssured.given() //precondition is to give the base uri .baseUri("http://localhost:8088/") .pathParam("employee_id", requestEmployeeId) .when() //actual action is to perform the delete request .delete("employees/{employee_id}") .then() // will return the ValidatadleResponse from the Response object .statusCode(200); } }
Lets go to our next tutorial where we will discuss below points :
Part 7 – JsonPath and XmlPath in Response Rest Assured
– What is JsonPath?
– What is XmlPath?
https://www.onlyfullstack.com/jsonpath-and-xmlpath-in-rest-assured/
Source Code
You can find the complete source code on below GitHub repository –
https://github.com/onlyfullstack/rest-assured-tutorial
Rest Assured Tutorial
https://www.onlyfullstack.com/rest-assured-tutorial-for-beginners/