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.

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:

  1. Create a new Student
  2. Verify Student is created
  3. Search the newly-created Student by id
  4. Update the created Student with new information
  5. Verify the search result
  6. Delete the created Student
  7. 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 {
@Test
public void testCreateUpdateDeleteStudent() {
// Creating Student with following details
CreateStudentRequest createStudentRequest = new CreateStudentRequest();
createStudentRequest.setGender("Female");
createStudentRequest.setFirstName("Sam");
createStudentRequest.setLastName("Bailey");
// Making Create Student web service call
CreateStudentResponse createStudentResponse = (CreateStudentResponse) webServiceTemplate.marshalSendAndReceive(
SERVICE_URL, createStudentRequest);
// Validating response is not null
assertNotNull(createStudentResponse, "CreateStudentResponse is null");
// Printing the response XML
printResponse(createStudentResponse);
// Created Student ID
long id = createStudentResponse.getStudent().getId();
GetStudentsRequest getStudentsRequest = new GetStudentsRequest();
getStudentsRequest.setId(id);
// Fetching the created Student response
GetStudentsResponse getStudentsResponse = (GetStudentsResponse) webServiceTemplate.marshalSendAndReceive(
SERVICE_URL, getStudentsRequest);
// Validating response is not null and contains more than one object
assertNotNull(getStudentsResponse, "GetStudentsResponse is null");
assertTrue(!getStudentsResponse.getStudents().isEmpty(), "students list is empty");
assertEquals(getStudentsResponse.getStudents().get(0).getFirstName(), "Sam", "first name");
// Printing the response XML
printResponse(getStudentsResponse);
// Update the Student with information
UpdateStudentRequest updateStudentRequest = new UpdateStudentRequest();
updateStudentRequest.setId(id);
updateStudentRequest.setGender("Male");
updateStudentRequest.setFirstName("Johnny");
updateStudentRequest.setLastName("Doe");
// Making UpdateStudent web service call
UpdateStudentResponse updateStudentResponse = (UpdateStudentResponse) webServiceTemplate.marshalSendAndReceive(
SERVICE_URL, updateStudentRequest);
// Validating response not null and the first name as given in the request
assertNotNull(updateStudentResponse, "UpdateStudentResponse is null");
assertEquals(updateStudentResponse.getStudent().getFirstName(), "Johnny");
// Printing the response XML
printResponse(updateStudentResponse);
// Fetching the created Student response
GetStudentsResponse getStudentsResponseAfterUpdate = (GetStudentsResponse) webServiceTemplate
.marshalSendAndReceive(SERVICE_URL, getStudentsRequest);
assertNotNull(getStudentsResponseAfterUpdate, "GetStudentsResponse is null");
assertTrue(!getStudentsResponseAfterUpdate.getStudents().isEmpty(), "students list is empty");
// Printing the response XML
printResponse(getStudentsResponseAfterUpdate);
// Deleting the Student
DeleteStudentRequest deleteStudentRequest = new DeleteStudentRequest();
deleteStudentRequest.setId(id);
// Making DeleteStudent web service call
DeleteStudentResponse deleteStudentResponse = (DeleteStudentResponse) webServiceTemplate.marshalSendAndReceive(
SERVICE_URL, deleteStudentRequest);
// Validating response is not null and isDeleted() true on successful delete
assertNotNull(deleteStudentResponse, "DeleteStudentResponse is null");
assertTrue(deleteStudentResponse.isDeleted(), "Student not deleted");
// Printing the response XML
printResponse(deleteStudentResponse);
// Fetching the deleted Student and expecting SoapFaultClientException
try {
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);
@BeforeSuite
public void init() {
if (CONTEXT == null) {
CONTEXT = new AnnotationConfigApplicationContext(io.educative.soap.WebServiceClient.class);
}
}
// Initializing WebServiceTemplate
@BeforeClass
public void initTemplate() {
webServiceTemplate = CONTEXT.getBean(WebServiceTemplate.class);
}
// Printing XML Response
protected 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 student
protected 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
...