Saturday 24 April 2021

How to Automate POST/PUT/Patch method with Rest Assured?



Check below link for question and answers with code:

Top API Interview Question 1-10

 

Let's learn about PUT, POST and PATCH.

PUT, PATCH, and POST are HTTP methods used to interact with a REST API.

PUT:
It is used to update a resource completely. It requires the client to send the complete resource representation. Example: Updating a user's profile information.

PATCH:
It is used to partially update a resource. It requires the client to send only the changes. Example: Updating a user's password.

POST:
It is used to create a new resource. It requires the client to send the representation of the resources to be created. Example: Creating a new user account.

Note:
For RESTful APIs, PUT method should only be used if the client can update the entire resource, while PATCH can be used if the client can update only a part of the resource.
The POST method should be used to create a new resource.


- First option: By using the entire JSON in an external file



- Second way: Using POJO class


First way: By using the entire JSON in an external file

1. IOUtils class will provide a method toString()

2. tostring will take two input param: Inputstream, encoding type

3. Fileinputstream will take param as File

4. File will require input param as the path where we kept .json file


@Test(description= "Automate post method for users")

public static void postmethodjson() throws IOException {


FileInputStream file = new FileInputStream(new File (System.getProperty("user.dir")+"\\Resources\\TestData\\testdata.json"));

Response resp = given().header("Content-Type","application/json").

body(IOUtils.toString(file, "UTF-8")).

when().post("https://reqres.in/api/users");

assertEquals(resp.getStatusCode(),201);

System.out.println("The status code for post method is : " + resp.getStatusCode() );

System.out.println("The response body of the post method is : " + resp.getBody().asString());

assertEquals(resp.path("job"),"leader");

}

Second way: Using pojo class

   How to create POJO class

How to automate with pojo class?

@Test

public static void postmethodwithpojoclass() {

String job = "tester";

String name = "automation";

pojoClass obj = new pojoClass(name,job);

Response resp = given().header("Content-Type","application/json").

body(obj).

when().post("https://reqres.in/api/users");

assertEquals(resp.getStatusCode(),201);

System.out.println("The status code for post method is : " + resp.getStatusCode() );

System.out.println("The response body of the post method is : " + resp.getBody().asString());

assertEquals(resp.path("job"),job);

assertEquals(resp.path("name"),name);

}

How to Create Fake-API which can be used for Testing in Postman or Rest Assured? Very Useful for real-time practice

 Ans: Steps with Screenshot to Create API from Scratch - Click Here



=========PATCH WITH POJO=======

@Test(groups="RegressionSuite")

pojoClass pjc = new pojoClass("morpheus", jobname);

Response resp = given().header("Content-Type","application/json").

          // body(requestParams.toString()).

            body(pjc).

           

    when().

            patch("https://reqres.in/api/users/2");

    System.out.println(resp.getBody().asString());

    assertEquals(resp.path("job"),jobname);

   

}


******If you are preparing for API Testing Interview then do refer the below list of Interview Q&A, which is prepared by industry leaders with 11+ years of experience: API Testing Interview Q&A **


https://automationreinvented.blogspot.com/2021/01/java-interview-questions-for.html

What are the status codes you have come across in your API testing project?

Ans: Click Here For Status Codes


Check below link for question and answers with code:

Top API Interview Question 1-10

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...