How to test PUT Request using Rest Assured

HOME

In the last tutorial, I have explained How to test POST Request using Rest Assured. In this tutorial, I will automate a PUT Request using Rest Assured. I will verify the status code, line of Status, and content of the Response.

To setup a basic Rest Assured Maven Project, click here and Gradle project, click here.

What is PUT Method?

The HTTP PUT API is primarily used to update existing resource. If the resource does not exist, then API may decide to create a new resource or not (Depends on API development). If a new resource has been created by the PUT API, the origin server MUST inform the user agent via the HTTP response code 201 (Created) response and if an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. To know more about Rest API, please click here.

Below are the steps to test a PUT Request using Rest Assured:

The steps to test the PUT request is similar to POST request. The only difference is that in POST we send a request to create a new resource whereas here we have a resource and I will update the detail of the already existing resource. To know about the steps and various imports used in the below example, please refer to the tutorial for POST Request.

Below is the response received for Employee with id 2.

I want to change the employee_salary to 99999. Below is the example for the test to update employee_salary.

public class PUT_NonBDDDemo {

	RequestSpecification requestSpecification;
	Response response;
	ValidatableResponse validatableResponse;

	@Test
	public void updateUser() {

		String jsonString = "{\"id\": 2,\r\n" 
                          + "        \"employee_name\": \"Garrett Winters\",\r\n"
				          + "        \"employee_salary\": 99999,\r\n"
                          + "        \"employee_age\": 63,\r\n"
				          + "        \"profile_image\": \"\"}";

		RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/update/2";

		// Create a request specification
		requestSpecification = RestAssured.given();

		// Setting content type to specify format in which request payload will be sent.
		requestSpecification.contentType(ContentType.JSON);

		// Adding body as string
		requestSpecification.body(jsonString);

		// Calling PUT method
		response = requestSpecification.put();

		// Let's print response body.
		String responseString = response.prettyPrint();

		/*
		 * To perform validation on response, we need to get ValidatableResponse type of
		 * response
		 */
		validatableResponse = response.then();

		// Get status code
		validatableResponse.statusCode(200);

		// It will check if status line is as expected
		validatableResponse.statusLine("HTTP/1.1 200 OK");

		// Check response - name attribute
		validatableResponse.body("data.employee_salary", equalTo(99999));

		// Check response - message attribute
		validatableResponse.body("message", equalTo("Successfully! Record has been updated."));

	}
}

Now, let us convert the same test in BDD format. In the below example, in the first part we have retrived the details of employee with id 2 and in second part we have updated the value of employee_salary to 99999.

@Test
	public void updateUser() {

	// To get the detail of employee with id 2
	validatableResponse = given()
                              .baseUri("http://dummy.restapiexample.com/api/v1/employee/2")
				              .contentType(ContentType.JSON)
                          .when()
                               .get()
                          .then()
                               .assertThat().statusCode(200);

		System.out.println("Response :" + validatableResponse.extract().asPrettyString());

		String jsonString = "{\"id\": 2,\r\n" 
                  + "        \"employee_name\": \"Garrett Winters\",\r\n"
				  + "        \"employee_salary\": 99999,\r\n"
                  + "        \"employee_age\": 63,\r\n"
				  + "        \"profile_image\": \"\"}";

	// Update employee_salary
	validatableResponse1 = given()
                               .baseUri("http://dummy.restapiexample.com/api/v1/update/2")
				               .contentType(ContentType.JSON)
                               .body(jsonString)
                           .when()
                               .put()
                           .then()
                               .assertThat().statusCode(200)
                              .body("data.employee_salary", equalTo(99999))
				              .body("message", equalTo("Successfully! Record has been updated."));
		
       System.out.println("Response :" + validatableResponse1.extract().asPrettyString());

	}

}

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s