Integration Test - REST
In this lesson, we will learn how to write an automated integration test using RESTful APIs for our demo web API.
We'll cover the following...
Integration scenario
In this scenario, we are simulating and automating the integration flow for a single service.
We will use our demo REST API to automate the below integration flow as follows:
- Create a new
Student
- Verify that
Student
is created - Search the newly-created
Student
byid
- Verify the search result
- Delete the created
Student
- Verify the
Student
has been deleted
Press + to interact
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.testng.annotations.Test;import static org.testng.Assert.assertEquals;import static org.testng.Assert.assertTrue;import com.fasterxml.jackson.annotation.JsonProperty;import io.restassured.RestAssured;import io.restassured.response.ResponseOptions;public class StudentIntegrationTest {private static Logger LOG = LoggerFactory.getLogger(StudentIntegrationTest.class);@Testpublic void testIntegrationFlow() {//Target resourceString url = "http://ezifyautomationlabs.com:6565/educative-rest/students";// Message body object.Student body = new Student("David", "Paul", "Male");//Step 1 - Create a new StudentResponseOptions<?> create = RestAssured.given().header("accept", "application/json").header("content-type", "application/json").body(body).post(url).andReturn();//print response message body in JSON formatLOG.info("Server response for created Student record : " +create.getBody().prettyPrint());//Step -2 Assert that request was successfulassertTrue(create.statusCode() == 201);long createdStudentId = create.getBody().jsonPath().getLong("id");//Step 3 - fetch the newly created Student detailsResponseOptions<?> fetch = RestAssured.given().get(url + "/" + createdStudentId).andReturn();//print response message body in JSON formatLOG.info("fetch newly created Student's record with id "+createdStudentId+ ":" +fetch.getBody().prettyPrint());//Step -4 parse the response data and verify and assert fetch Student detailsassertTrue(fetch.statusCode() == 200);long studentID = fetch.getBody().jsonPath().getLong("id");assertEquals(createdStudentId,studentID);//Step 5 - Delete the newly created Student ObjectLOG.info("Delete Student with id "+studentID+"'s record");ResponseOptions<?> delete = RestAssured.given().delete(url + "/" + studentID);//Assert request was successfulassertTrue(delete.statusCode() == 204);// Step 6 - Try getting Deleted Student's recordResponseOptions<?> deletedStudent = RestAssured.given().get(url + "/" + createdStudentId).andReturn();//Assert record is deleted - NO record foundLOG.info("HTTP GET response statusLine of deleted record "+deletedStudent.getStatusLine());assertTrue(deletedStudent.statusCode() == 404);}}// This POJO class will be used for serialization and deserialzation of the dataclass Student {public Student(String firstName, String lastName, String gender) {this.firstName = firstName;this.lastName = lastName;this.gender = gender;}@JsonProperty("id")Long id;@JsonProperty("first_name")String firstName;@JsonProperty("last_name")String lastName;@JsonProperty("gender")String gender;}
Understanding the code
Since the code above has enough in-line comments, we will only discuss some ...