Integration Test - SOAP
In this lesson, we will learn how to call a SOAP web service, chain multiple web service calls, automate and write an integration test.
We'll cover the following...
Integration scenario
In this scenario, we are simulating and automating the integration flow of a single service.
We will use our demo SOAP web services to automate the below integration flow as follows:
- Create a new
Student
- Verify
Student
is created - Search the newly-created
Student
byid
- Update the created
Student
with new information - Verify the search result
- Delete the created
Student
- Verify the
Student
is deleted
Press + to interact
import static org.testng.Assert.assertEquals;import static org.testng.Assert.assertNotNull;import static org.testng.Assert.assertTrue;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.ws.client.core.WebServiceTemplate;import org.springframework.ws.soap.client.SoapFaultClientException;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeSuite;import org.testng.annotations.Test;import com.fasterxml.jackson.dataformat.xml.XmlMapper;import io.educative.soap_automation.CreateStudentRequest;import io.educative.soap_automation.CreateStudentResponse;import io.educative.soap_automation.DeleteStudentRequest;import io.educative.soap_automation.DeleteStudentResponse;import io.educative.soap_automation.GetStudentsRequest;import io.educative.soap_automation.GetStudentsResponse;import io.educative.soap_automation.UpdateStudentRequest;import io.educative.soap_automation.UpdateStudentResponse;public class TestSOAP extends BaseTest {@Testpublic void testCreateUpdateDeleteStudent() {// Creating Student with following detailsCreateStudentRequest createStudentRequest = new CreateStudentRequest();createStudentRequest.setGender("Female");createStudentRequest.setFirstName("Sam");createStudentRequest.setLastName("Bailey");// Making Create Student web service callCreateStudentResponse createStudentResponse = (CreateStudentResponse) webServiceTemplate.marshalSendAndReceive(SERVICE_URL, createStudentRequest);// Validating response is not nullassertNotNull(createStudentResponse, "CreateStudentResponse is null");// Printing the response XMLprintResponse(createStudentResponse);// Created Student IDlong id = createStudentResponse.getStudent().getId();GetStudentsRequest getStudentsRequest = new GetStudentsRequest();getStudentsRequest.setId(id);// Fetching the created Student responseGetStudentsResponse getStudentsResponse = (GetStudentsResponse) webServiceTemplate.marshalSendAndReceive(SERVICE_URL, getStudentsRequest);// Validating response is not null and contains more than one objectassertNotNull(getStudentsResponse, "GetStudentsResponse is null");assertTrue(!getStudentsResponse.getStudents().isEmpty(), "students list is empty");assertEquals(getStudentsResponse.getStudents().get(0).getFirstName(), "Sam", "first name");// Printing the response XMLprintResponse(getStudentsResponse);// Update the Student with informationUpdateStudentRequest updateStudentRequest = new UpdateStudentRequest();updateStudentRequest.setId(id);updateStudentRequest.setGender("Male");updateStudentRequest.setFirstName("Johnny");updateStudentRequest.setLastName("Doe");// Making UpdateStudent web service callUpdateStudentResponse updateStudentResponse = (UpdateStudentResponse) webServiceTemplate.marshalSendAndReceive(SERVICE_URL, updateStudentRequest);// Validating response not null and the first name as given in the requestassertNotNull(updateStudentResponse, "UpdateStudentResponse is null");assertEquals(updateStudentResponse.getStudent().getFirstName(), "Johnny");// Printing the response XMLprintResponse(updateStudentResponse);// Fetching the created Student responseGetStudentsResponse getStudentsResponseAfterUpdate = (GetStudentsResponse) webServiceTemplate.marshalSendAndReceive(SERVICE_URL, getStudentsRequest);assertNotNull(getStudentsResponseAfterUpdate, "GetStudentsResponse is null");assertTrue(!getStudentsResponseAfterUpdate.getStudents().isEmpty(), "students list is empty");// Printing the response XMLprintResponse(getStudentsResponseAfterUpdate);// Deleting the StudentDeleteStudentRequest deleteStudentRequest = new DeleteStudentRequest();deleteStudentRequest.setId(id);// Making DeleteStudent web service callDeleteStudentResponse deleteStudentResponse = (DeleteStudentResponse) webServiceTemplate.marshalSendAndReceive(SERVICE_URL, deleteStudentRequest);// Validating response is not null and isDeleted() true on successful deleteassertNotNull(deleteStudentResponse, "DeleteStudentResponse is null");assertTrue(deleteStudentResponse.isDeleted(), "Student not deleted");// Printing the response XMLprintResponse(deleteStudentResponse);// Fetching the deleted Student and expecting SoapFaultClientExceptiontry {webServiceTemplate.marshalSendAndReceive(SERVICE_URL, getStudentsRequest);} catch (SoapFaultClientException e) {assertEquals(e.getMessage(), String.format("student with id '%s' not found", id));}}}abstract class BaseTest {protected static ApplicationContext CONTEXT;protected WebServiceTemplate webServiceTemplate;protected static final String SERVICE_URL = "http://ezifyautomationlabs.com:6566/educative-soap/ws";protected static final Logger LOG = LoggerFactory.getLogger(BaseTest.class);@BeforeSuitepublic void init() {if (CONTEXT == null) {CONTEXT = new AnnotationConfigApplicationContext(io.educative.soap.WebServiceClient.class);}}// Initializing WebServiceTemplate@BeforeClasspublic void initTemplate() {webServiceTemplate = CONTEXT.getBean(WebServiceTemplate.class);}// Printing XML Responseprotected void printResponse(Object response) {try {LOG.info("printing response '{}' => \n{}", response.getClass().getName(),new XmlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(response));} catch (Exception e) {e.printStackTrace();}}// An utility to create studentprotected long createStudent() {CreateStudentRequest request = new CreateStudentRequest();request.setGender("Male");request.setFirstName("John");request.setLastName("Doe");CreateStudentResponse response = (CreateStudentResponse) webServiceTemplate.marshalSendAndReceive(SERVICE_URL,request);assertNotNull(response, "CreateStudentResponse is null");return response.getStudent().getId();}}
Understanding the code
Since the code above has enough in-line comments, we will only discuss some important code snippets here.
The web service is hosted at http://ezifyautomationlabs.com:6566/educative-soap/ws
and saved as SERVICE_URL
in BaseTest
.
It uses the TestNG
library to write the test method.
1. Create a new student
// Creating Student with following details
CreateStudentRequest createStudentRequest = new CreateStudentRequest();
createStudentRequest.setGender("Female");
createStudentRequest.setFirstName("Sam");
createStudentRequest.setLastName("Bailey");
In this piece of code, we create the CreateStudentRequest
request object to make the CreateStudent
web service call.
// Making Create Student web
...