Example of GET Request:
RUN Manually By PostMan:
Required dependency like jayway.restassured,junite,jackson-databind :
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 10 Mar 2019 09:04:28 GMT
[
{
"id": 1,
"firstName": "Vernon",
"lastName": "Harper",
"email": "egestas.rhoncus.Proin@massaQuisqueporttitor.org",
"programme": "Financial Analysis",
"courses": [
"Accounting",
"Statistics"
]
},
{
"id": 2,
"firstName": "Murphy",
"lastName": "Holmes",
"email": "faucibus.orci.luctus@Duisac.net",
"programme": "Financial Analysis",
"courses": [
"Accounting",
"Statistics"
]
}
]
com.jayway.restassured.internal.RestAssuredResponseImpl@21a21c64
Process finished with exit code 0
Run Same Api Manually on Postman:
2. Example of POST Request:
let start post operation manually with postman tool.IN postman tool to perform post operation we have to select POST options in dropdown(marked in red in below picture).enter api url like:http://localhost:8080/student
and pass input payload like below:
input payload:
{ "firstName": "Monika1", "lastName": "Rani1", "email": "monika11@test.org", "programme":"Computer Science", "courses":[ "Java", "Python", "TypeScript"] }
output:
Automation Script for POST operation:
step:1.create one student pojo class:
student class code:
RUN Manually By PostMan:
Required dependency like jayway.restassured,junite,jackson-databind :
Rest Assured script:
package com.students.tests; import com.jayway.restassured.RestAssured; import com.jayway.restassured.RestAssured.*; import com.jayway.restassured.response.Response; import org.junit.BeforeClass; import org.junit.Test; import static com.jayway.restassured.RestAssured.*; public class StudentGetTest { @BeforeClass public static void init() { RestAssured.baseURI = "http://localhost"; RestAssured.port = 8080; RestAssured.basePath="/student"; } @Test public void getAllStudent() { Response response=given() .when() .get("/list"); // System.out.println("response :"+response.body().prettyPrint());given().when().then().statusCode(500); } @Test public void getStudentInfo() { Response response=given() .when() .get("/1"); System.out.println("response :"+response.body().prettyPrint()); // given().when().then().statusCode(500); given().when().then().statusCode(500); } @Test public void getStudentFromFA() { Response response = given() .when() .get("/list?programme=Financial Analysis&limit=2"); // System.out.println(response.prettyPeek()); Response response2 = given().param("programme","Financial Analysis") .param("limit","2") .when() .get("/list"); System.out.println(response2.prettyPeek()); } }
Run The Test:
Output:
Transfer-Encoding: chunked
Date: Sun, 10 Mar 2019 09:04:28 GMT
[
{
"id": 1,
"firstName": "Vernon",
"lastName": "Harper",
"email": "egestas.rhoncus.Proin@massaQuisqueporttitor.org",
"programme": "Financial Analysis",
"courses": [
"Accounting",
"Statistics"
]
},
{
"id": 2,
"firstName": "Murphy",
"lastName": "Holmes",
"email": "faucibus.orci.luctus@Duisac.net",
"programme": "Financial Analysis",
"courses": [
"Accounting",
"Statistics"
]
}
]
com.jayway.restassured.internal.RestAssuredResponseImpl@21a21c64
Process finished with exit code 0
Run Same Api Manually on Postman:
2. Example of POST Request:
let start post operation manually with postman tool.IN postman tool to perform post operation we have to select POST options in dropdown(marked in red in below picture).enter api url like:http://localhost:8080/student
and pass input payload like below:
input payload:
{ "firstName": "Monika1", "lastName": "Rani1", "email": "monika11@test.org", "programme":"Computer Science", "courses":[ "Java", "Python", "TypeScript"] }
output:
Automation Script for POST operation:
step:1.create one student pojo class:
student class code:
package com.student.model; import java.util.List; public class Student { private String firstName; private String lastName; private String email; private String programme; private List<String> courses; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getProgramme() { return programme; } public void setProgramme(String programme) { this.programme = programme; } public List<String> getCourses() { return courses; } public void setCourses(List<String> courses) { this.courses = courses; } }
step2: create test class for post(studenrPostTest.java)
studenrPostTest.java:
package com.students.tests; import static org.hamcrest.Matchers.*; import static com.jayway.restassured.RestAssured.*; import com.jayway.restassured.RestAssured; import com.jayway.restassured.RestAssured.*; import com.jayway.restassured.http.ContentType; import com.jayway.restassured.response.Response; import com.student.model.Student; import org.junit.BeforeClass; import org.junit.Test; import java.util.ArrayList; import static com.jayway.restassured.RestAssured.*; public class StudentPostTest { @BeforeClass public static void init(){ RestAssured.baseURI = "http://localhost"; RestAssured.port = 8080; RestAssured.basePath="/student"; } @Test public void createNewStudent(){ ArrayList<String> courses = new ArrayList<String>(); courses.add("Java"); courses.add("typescript"); Student student = new Student(); student.setFirstName("Monika"); student.setLastName("Rani"); student.setEmail("monika@gmail.com"); student.setProgramme("Computer Science"); student.setCourses(courses); given() .contentType(ContentType.JSON) .when() .body(student) .post() .then() .statusCode(201); } }Run the test:output:verify that record inserted or created by postmethod or not:we will perform get operation on postman and verify created record:
Good stuff. Keep doing this work.
ReplyDeleteNice. Good information. Keep up the good work.
ReplyDeleteNice to see you active again after 3 year ...
ReplyDelete