Table of Contents
How to send PUT Request in Rest Assured
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/
What is HTTP PUT Request?
The HTTP PUT request method replaces a representation of the target resource with the request payload. In our employee rest api you can hit the Update Employee request to update the details of the employees. For this you need to pass the body in you PUT request
Update Employee –
HTTP Method – PUT
URL – http://localhost:8088/employees/2
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 –
{
“firstName” : “XYZ”,
“lastName” : “ABC”,
“email” : “xyzabc@gmail.com”,
“salary” : “10000”
}
Lets write code to automate this put request and check the output –
Rest assured put api automation without BDD approach
@Test public void putMethodWithoutBDDApproach() { String requestBody = " {n" + ""firstName": "Shivansh",n" + ""lastName": "Oza",n" + ""salary": 5000,n" + ""email": "shivansh@abc.com"n" + "}"; String requestEmployeeId = "1"; RequestSpecification request = RestAssured.given() .pathParam("employee_id", requestEmployeeId); request.baseUri("http://localhost:8088/"); request.body(requestBody); request.contentType(ContentType.JSON); Response response = request.put("employees/{employee_id}"); // Let's print response body. String resString = response.asString(); System.out.println("Response Details : " + resString); /* * 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); // It will check if status line is as expected validatableResponse.body("id", Matchers.notNullValue()); validatableResponse.body("firstName", Matchers.equalTo("Shivansh")); validatableResponse.body("lastName", Matchers.equalTo("Oza")); validatableResponse.body("salary", Matchers.equalTo(5000)); validatableResponse.body("email", Matchers.equalTo("shivansh@abc.com")); }
As we have seen in the http put 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. 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 put method
Before calling the put method we should pass the body(json of the employee we want to update)
RestAssured has a static overloaded method named put(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.
request.body(requestBody); request.contentType(ContentType.JSON); Response response = request.put("/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 and status line. It consists of many validation methods.
In below code we are first validating the status code and then we are validating the updated employee record and comparing all the properties with expected value. We are using the JsonPath to retrieve the value from the response json.
Don’t worry about the JsonPath. We will learn about them in our upcoming blog.
body method takes 2 parameters, first is the son path for which the value you have to compare nd second one is the Matcher which has lots of method to compare the value.
T body(String var1, Matcher<?> var2, Object... var3); /* * 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); // It will check if status line is as expected validatableResponse.body("id", Matchers.notNullValue()); validatableResponse.body("firstName", Matchers.equalTo("Shivansh")); validatableResponse.body("lastName", Matchers.equalTo("Oza")); validatableResponse.body("salary", Matchers.equalTo(5000)); validatableResponse.body("email", Matchers.equalTo("shivansh@abc.com"));
Rest assured http put 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 postMethodWithBDDApproach() { String requestBody = " {n" + ""firstName": "Suresh",n" + ""lastName": "Oza",n" + ""salary": 10000,n" + ""email": "suresh@abc.com"n" + "}"; String requestEmployeeId = "2"; RestAssured.given() .baseUri("http://localhost:8088") .pathParam("employee_id", requestEmployeeId) .body(requestBody) .contentType(ContentType.JSON) .when() .put("/employees/{employee_id}") .then() .statusCode(201) .body("id", Matchers.notNullValue()) .body("firstName", Matchers.equalTo("Suresh")) .body("lastName", Matchers.equalTo("Oza")) .body("salary", Matchers.equalTo(10000)) .body("email", Matchers.equalTo("suresh@abc.com")); }
Below is the complete program for HTTP post method with rest assured
package onlyfullstack.httpMethods; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; import org.hamcrest.Matchers; import org.testng.annotations.Test; public class PutHttpMethodExample { @Test public void putMethodWithoutBDDApproach() { String requestBody = " {n" + ""firstName": "Shivansh",n" + ""lastName": "Oza",n" + ""salary": 5000,n" + ""email": "shivansh@abc.com"n" + "}"; String requestEmployeeId = "1"; RequestSpecification request = RestAssured.given() .pathParam("employee_id", requestEmployeeId); request.baseUri("http://localhost:8088/"); request.body(requestBody); request.contentType(ContentType.JSON); Response response = request.put("employees/{employee_id}"); // Let's print response body. String resString = response.asString(); System.out.println("Response Details : " + resString); /* * 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); // It will check if status line is as expected validatableResponse.body("id", Matchers.notNullValue()); validatableResponse.body("firstName", Matchers.equalTo("Shivansh")); validatableResponse.body("lastName", Matchers.equalTo("Oza")); validatableResponse.body("salary", Matchers.equalTo(5000)); validatableResponse.body("email", Matchers.equalTo("shivansh@abc.com")); } @Test public void putMethodWithBDDApproach() { String requestBody = " {n" + ""firstName": "Suresh",n" + ""lastName": "Oza",n" + ""salary": 10000,n" + ""email": "suresh@abc.com"n" + "}"; String requestEmployeeId = "2"; RestAssured.given() .baseUri("http://localhost:8088") .pathParam("employee_id", requestEmployeeId) .body(requestBody) .contentType(ContentType.JSON) .when() .post("/employees/{employee_id}") .then() .statusCode(200) .body("id", Matchers.notNullValue()) .body("firstName", Matchers.equalTo("Suresh")) .body("lastName", Matchers.equalTo("Oza")) .body("salary", Matchers.equalTo(10000)) .body("email", Matchers.equalTo("suresh@abc.com")); } }
Lets go to our next tutorial where we will discuss below points :
Part 6 – How to make a DELETE Request using Rest Assured
– What is DELETE?
– How to make a DELETE Request using Rest Assured?
– Rest assured http delete request api automation without BDD approach
– Rest assured get api automation with BDD approach
https://www.onlyfullstack.com/how-to-send-delete-request-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/