-
Notifications
You must be signed in to change notification settings - Fork 0
Tool Java_Library:RestAssured
Rajendra Prasad Reddy Penumalli edited this page Mar 21, 2020
·
2 revisions
- Popular and Widely used java library for RestAPI Automation
- We can utilize the advantages of Java or Groovy
- Doesn’t have any limitations and can be combined with different frameworks.
- Easy integration with existing framework and customization
- Library provides an option of :
- writing any tests
- performing any manipulations with data during the testing process leaving tests independent
- Opportunity to apply ready-made tools without connecting additional libraries.
- Comprehensive documentation is available on Github.
- Extensive testing options in combination with different frameworks for testing and reports building.
- Tests writing in the form of code using Java or Groovy with an option to update, improve the code’s quality or the number of tests.
- Independence from the platform or development environment.
- The necessity of having a basic knowledge of Java, Groovy, Maven and the experience of working with dependencies, command line and IDE.
- Code quality and time necessary for compilation directly impact test running speed.
- No GUI as for Postman and Katalon Studio.
- A tester has to connect and set up additional tools for reports building.
- It will take some time to check general API working efficiency and spend time on test writing.
- Java or Groovy
- RestAssured
- TestNG
- GSON
- Log4j
- RestAssured
- Junit
- GSON
- Log4j
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class SampleAPITest {
@Test
public void GetEmpDetails()
{
RestAssured.baseURI = "http://192.168.20.150:3000";
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
Response response = request.get("/employees/1");
String responseBody = response.getBody().asString();
System.out.println("Response Body is => " + responseBody);
}
}
public class SampleAPITest {
@Test
public void PostEmpDetails()
{
RestAssured.baseURI = "http://192.168.20.150:3000";
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
JSONObject requestParams = new JSONObject();
requestParams.put("name", "Rajendra");
requestParams.put("salary", "20000")
request.body(requestParams.toJSONString());
Response response = request.post("/employees");
String responseBody = response.getBody().asString();
System.out.println("Response Body is => " + responseBody);
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, "201");
String successCode = response.jsonPath().get("SuccessCode");
Assert.assertEquals( "Correct Success code was returned", successCode, "OPERATION_SUCCESS");
}
}
public class SampleAPITest {
@Test
public void PutEmpDetails()
{ int empid=122;
RestAssured.baseURI = "http://192.168.20.150:3000";
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
JSONObject requestParams = new JSONObject();
requestParams.put("name", "Rajendra");
requestParams.put("salary", "21000")
request.body(requestParams.toJSONString());
Response response = request.put("/employees/"+ empid);
int statusCode = response.getStatusCode();
System.out.println(response.asString());
Assert.assertEquals(statusCode, 200);
}
}
public class SampleAPITest {
@Test
public void DeleteEmpDetails()
{ int empid=122;
RestAssured.baseURI = "http://192.168.20.150:3000";
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
Response response = request.delete("/employees/"+ empid);
int statusCode = response.getStatusCode();
System.out.println(response.asString());
Assert.assertEquals(statusCode, 200);
String jsonString =response.asString();
Assert.assertEquals(jsonString.contains("successfully! deleted Records"), true);
}
}
- Get Request with query params: http://192.168.20.150:3000/employees?name=rajendra&age=15
public class SampleAPITest {
@Test
public void GetEmpDetails()
{
RestAssured.baseURI = "http://192.168.20.150:3000";
RequestSpecification request = RestAssured.given();
request.param("name", "rajendra").param("age", "15");
request.header("Content-Type", "application/json");
Response response = request.get("/employees/1");
String responseBody = response.getBody().asString();
System.out.println("Response Body is => " + responseBody);
}
}