Rest Assured

Delete Method in Rest Assured

Here is the simple example to perform delete in rest assured.

package session02;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

public class TestDeleteMethod {
  @Test
  public void test05() {
	  RestAssured.baseURI = "https://reqres.in/api/users/52";
	  RestAssured.given()
	             .when()
	                 .delete()
	             .then()
	                 .statusCode(204).log().all();
  }
}
Uncategorized

Put and Patch method in Rest Assured

Difference Between Put and Patch Method

What is Put?

When creating an object or resource on an HTTP server, clients utilize the PUT method. This setup procedure itself may take one of two shapes:

When an object is requested that doesn’t already exist, the server creates it and returns a response code 201 to the client.

The server changes the entity, which already exists, and returns a success code of 200 and 204 to a client. Moreover, the server should provide the client with the appropriate error number, often a 4xx or 5xx, if a PUT request encounters a problem.

What is Patch?

The PATCH technique modifies resource elements only partially. The requested modifications are executed atomically via the PATCH technique. It implies that the server won’t alter the target object if it can’t accommodate all of the requested modifications. In this approach, if the request is carried out correctly, the server sends the client the success code 204. Otherwise, an error code is sent by the server.

Here is simple example in Rest Assured for Put and Patch

package session02;

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class TestPutMethod {
  @Test
  public void test04() {
	  JSONObject jsonData = new JSONObject();
	  jsonData.put("Name", "Rahul kundu");
	  jsonData.put("job", "manager");
	  
	  RestAssured.baseURI = "https://reqres.in/api/users/52";
	  RestAssured.given().header("Content-Type","application/json").contentType(ContentType.JSON)
	             .body(jsonData.toJSONString())
	             .when().put()
	             .then().statusCode(200).log().all();
  }
  @Test
  public void test05() {
	  JSONObject jsonData = new JSONObject();
	  jsonData.put("Name", "Rahul kundu");
	  jsonData.put("job", "QA manager");
	  
	  RestAssured.baseURI = "https://reqres.in/api/users/52";
	  RestAssured.given().header("Content-Type","application/json").contentType(ContentType.JSON)
	             .body(jsonData.toJSONString())
	             .when().patch()
	             .then().statusCode(200).log().all();
  }
}
Uncategorized

Post Method in Rest Assured

Simple Example for Post method.

You have to add Json simple and rest assured dependency in your maven project.

JSON Simple

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Rest Assured

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.0</version>
    <scope>test</scope>
</dependency>

Example of Post Method:

package session02;

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class TestPostMentod {
  @Test
  public void test04() {
	  
	  JSONObject jsonData = new JSONObject();
	  jsonData.put("Name", "Rahul");
	  jsonData.put("job", "QA Lead");
	  
	  RestAssured.baseURI = "https://reqres.in/api/users";
	  RestAssured.given().header("Content-Type","application/json").contentType(ContentType.JSON)
	             .body(jsonData.toJSONString())
	             .when().post()
	             .then().statusCode(201);
  }
}
Uncategorized

Get Request in Rest Assured

Below is the simple Get Request in Rest Assured.


package session01;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class GetRequest {
	
  @Test
  public void test01() {
	  
	  //RestAssured.baseURI = "https://reqres.in/api/users/2";
	  Response response = RestAssured.get("https://reqres.in/api/users/2");
	  System.out.println(response.asString());
	  System.out.println(response.getStatusCode());
	  
  }
}
Uncategorized

Get Request in Rest Assured

Create new Maven Project, add the following dependencies in your pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.test.RestAssuredAPITesting</groupId>

<artifactId>RestAssuredAPITesting</artifactId>

<version>0.0.1-SNAPSHOT</version>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>7.7.1</version>

<scope>test</scope>

</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>rest-assured</artifactId>

<version>5.3.0</version>

<scope>test</scope>

</dependency>

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->

<dependency>

<groupId>com.googlecode.json-simple</groupId>

<artifactId>json-simple</artifactId>

<version>1.1.1</version>

</dependency>

</dependencies>

</project>

Create a new TestNG class and following code:

package session1;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

import io.restassured.response.Response;

public class GetRequest {

@Test

public void test01() {

//RestAssured.baseURI = "https://reqres.in/api/users/2";

Response response = RestAssured.get("https://reqres.in/api/users/2");

System.out.println(response.asString());

System.out.println(response.getStatusCode());

}

}