[go: up one dir, main page]

0% found this document useful (0 votes)
20 views11 pages

Rest Assured Using JAVA For SDETs

Uploaded by

Praveen Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Rest Assured Using JAVA For SDETs

Uploaded by

Praveen Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Rest Assured Setup from

Scratch for SDETs


Created by: Japneet Sachdeva
HTTP Methods for APIs
GET
Retrieves the information at a particular URL.
PUT
Updates the previous resource if it exists or creates new information at a particular URL.
POST
Used to send information to the server like uploading data and also to develop a new entity.
DELETE
Deletes all current representations at a specific URL.
PATCH
This is used for partial updates of resources.

LinkedIn: Japneet Sachdeva


HTTP Status Codes
Once the Request is received by the server, it sends a response back using Response codes and Response body (if required).

1xx (100 – 199): The response is informational.

2xx (200 – 299): Assures successful response.

3xx (300 – 399): You are required to take further action to fulfill the request.

4xx (400 – 499): There's bad syntax and the request cannot be completed.

5xx (500 – 599): The server entirely fails to complete the request.

LinkedIn: Japneet Sachdeva


Code Setup for Rest Assured using JAVA
Now let’s set up a Rest Assured Tests from scratch using JAVA

@BeforeClass
public void setup() {
// Set the base URI for Rest Assured
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}

LinkedIn: Japneet Sachdeva


GET Request
@Test
public void testGetRequest() {
// Create a request specification
RequestSpecification request = RestAssured.given();

// Send the GET request and receive the response


Response response = request.get("/posts/1");

// Assert the response status code and body


Assert.assertEquals(response.getStatusCode(), 200, "Status code should be 200");
System.out.println("GET Response Body: " + response.getBody().asString());
}

LinkedIn: Japneet Sachdeva


POST Request
The test creation process remains same, rest we can use below code to setup the request quickly

// Create a request specification


RequestSpecification request = RestAssured.given();

// Set the header to specify that the request body is JSON


request.header("Content-Type", "application/json");

// Set the request body


String requestBody = "{\n" +
" \"title\": \"foo\",\n" +
" \"body\": \"bar\",\n" +
" \"userId\": \"1\"\n" +
"}";

// Send the POST request and receive the response


Response response = request.body(requestBody).post("/posts");

// Assert the response status code and body


Assert.assertEquals(response.getStatusCode(), 201, "Status code should be 201");
System.out.println("POST Response Body: " + response.getBody().asString());

LinkedIn: Japneet Sachdeva


PUT Request
The test creation process remains same, rest we can use below code to setup the request quickly

// Create a request specification


RequestSpecification request = RestAssured.given();

// Set the header to specify that the request body is JSON


request.header("Content-Type", "application/json");

// Set the request body


String requestBody = "{\n" +
" \"id\": \"1\",\n" +
" \"title\": \"foo\",\n" +
" \"body\": \"bar\",\n" +
" \"userId\": \"1\"\n" +
"}";

// Send the PUT request and receive the response


Response response = request.body(requestBody).put("/posts/1");

// Assert the response status code and body


Assert.assertEquals(response.getStatusCode(), 200, "Status code should be 200");
System.out.println("PUT Response Body: " + response.getBody().asString());

LinkedIn: Japneet Sachdeva


DELETE Request
// Create a request specification
RequestSpecification request = RestAssured.given();

// Send the DELETE request and receive the response


Response response = request.delete("/posts/1");

// Assert the response status code


Assert.assertEquals(response.getStatusCode(), 200, "Status code should be 200");

LinkedIn: Japneet Sachdeva


Code Explanation
@BeforeClass: Runs before any test in the class
@Test: Each Request is treated as a test to facilitate further reporting etc.
Assert.assertEquals: Compares actual result with expected. Fails if they do not match.
setup(): Used to initialize baseURI, we can also use this function for setting global properties which remain the same throughout all
tests.

RestAssuredRestAssured.baseURI: Root or baseURL for all the APIs used for tests.

RequestSpecification: This interface allows you to specify how the request should look like. You can add headers, cookies, body, etc.,
to the request

given(): Helps in building the api request


header(): APIs have different types of header so this method can be used to set it.
body(): used for PUT & POST request majorly

LinkedIn: Japneet Sachdeva


Code Explanation Continued..
get(), post(), put(), delete(): all are used for respective HTTP methods GET, POST, PUT & DELETE

getStatusCode(): used to retrieve the response code


getBody().asString(): used to retrieve response body in string format

Dependencies Used:

<!-- Rest Assured --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId>


<version>4.5.0</version> </dependency>
<!-- TestNG -->
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.7.1</version> <scope>test</scope>
</dependency>
<!-- Gson for JSON parsing --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId>
<version>2.8.9</version> </dependency>

LinkedIn: Japneet Sachdeva


Follow me on:

@LinkedIn: Japneet Sachdeva

@YouTube: Japneet Sachdeva

@Medium: Japneet Sachdeva

@Udemy: Japneet Sachdeva

@TopMate: Japneet Sachdeva

You might also like