Introduction
SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in the implementation of web services. It employs XML for message format definition and depends on transport protocols like HTTP, SMTP, TCP, or others for message transmission.
Discover the robust capabilities of Rest Assured, a potent Java library that streamlines SOAP web service testing. Follow our detailed guide and practical examples to automate SOAP API tests efficiently. Enhance your testing efficiency and guarantee the dependability of your web services through this comprehensive tutorial. Initiate your journey today and optimise your API automation workflow with Rest Assured.
In this blog post, we will explore the distinctions between REST and SOAP, followed by a comprehensive guide on automating SOAP APIs using Rest Assured.
REST vs SOAP
Characteristic | SOAP (Simple Object Access Protocol) | REST (Representational State Transfer) |
Protocol | Protocol-based, usually over HTTP, SMTP, or others. | Architectural style using HTTP primarily. |
Standards | Strict standards and specifications. | Flexible and not bound to strict standards. |
Message Format | XML is the standard message format, but it can also use JSON. | Supports various formats, including XML, JSON, and more. |
Verbosity | Typically verbose due to XML. | Lightweight and less verbose, often using JSON. |
Communication | Supports request-response and request-only patterns. | Supports multiple communication methods, including CRUD. |
Error Handling | Well-defined error handling using fault messages. | Typically relies on HTTP status codes for error handling. |
State Management | Supports stateful communication using WS-Security, WS-ReliableMessaging, etc. | Stateless, each request is independent. |
Security | Built-in security through WS-Security, often complex. | Relies on underlying protocols (HTTPS) and additional measures. |
Performance | Can be slower due to XML parsing and verbosity. | Generally faster due to simplicity and lightweight nature. |
Caching | Limited caching options. | Supports caching through HTTP headers. |
Scalability | Scalability can be a challenge in some cases. | Easier to scale due to statelessness and simplicity. |
Usage | Often used in enterprise-level applications and services. | Widely used in web services, APIs, and web applications. |
SOAP Example
<SOAP-ENV:Envelope xmlns:SOAP-ENV=”http://www.w3.org/2003/05/soap-envelope” xmlns:ns1=”http://example.com”>
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns1:GetUser>
<ns1:UserID>123</ns1:UserID>
</ns1:GetUser>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
REST Example (HTTP GET request)
GET /users/123 HTTP/1.1
Host: api.example.com
How to automate SOAP APIs using Rest Assured with real-time examples
Rest Assured is a powerful Java library that simplifies API testing for SOAP web services. By adhering to our detailed instructions and hands-on examples, you can effectively streamline the automation of your SOAP API tests. Boost your testing productivity and ensure the reliability of your web services with this comprehensive tutorial. Get started today and
streamline your API automation process with Rest Assured.
To achieve SOAP API automation using RestAssured, you must establish project dependencies, configure the testing environment, and craft test scripts. The following is an illustrative example of this process:
Step 1: Set up the Project
Set up the Project Create a new Java project in your favourite IDE and add the required dependencies. In this example, we’ll use Maven for dependency management. Add the dependencies mentioned in the below to your pom.xml file:
<dependencies> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.3.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> </dependencies> |
Step 2: Configure the Test Environment
Configure the Test Environment Create a new Java class for your test and configure the base URI for the SOAP API:
import io.restassured.RestAssured; import org.testng.annotations.BeforeClass; public class SoapAPITest { @BeforeClass public void setUp() { RestAssured.baseURI = “https://example.com/soap-api”; // Replace with your SOAP API URL } } |
Step 3: Write Test Scripts
Now you can start writing test scripts to interact with the SOAP API using RestAssured. Below is an example of how to make a SOAP request and validate the response:
import io.restassured.response.Response; import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; public class SoapAPITest extends BaseTest { @Test public void testSoapRequest() { String soapRequest = “<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\” “ + “xmlns:web=\”http://www.example.com/webservice\”>” + “<soapenv:Header/>” + “<soapenv:Body>” + “<web:YourSoapRequest>” + “<web:Parameter1>Value1</web:Parameter1>” + “<web:Parameter2>Value2</web:Parameter2>” + “</web:YourSoapRequest>” + “</soapenv:Body>” + “</soapenv:Envelope>”; Response response = given() .contentType(“text/xml”) .body(soapRequest) .when() .post(“/your-soap-endpoint”) // Replace with your SOAP endpoint .then() .statusCode(200) .body(“Envelope.Body.YourSoapResponse”, equalTo(“ExpectedValue”)) // Replace with your expected value .extract().response(); // Print the SOAP response System.out.println(response.getBody().asString()); } } |
Note: In the above example, you need to replace the placeholders (e.g., https://example.com/soap-api, /your-soap-endpoint, Value1, Value2, ExpectedValue) with your actual SOAP API URL, endpoint, request parameters, and expected values.
With the above setup and test script, you can now run your RestAssured tests to automate SOAP API testing efficiently.
Real time API example with SOAP
To make SOAP API (http://www.dneonline.com/calculator.asmx?WSDL) automation using Rest Assured is easier, you should use the Rest Assured library along with the SAAJ (SOAP with Attachments API for Java) library.
Rest Assured doesn’t have direct support for handling SOAP, so we’ll use SAAJ to handle the SOAP request and response. Below is a Java code example that shows how to do an ‘Add’ operation using Rest Assured and SAAJ:
import org.testng.annotations.Test; import io.restassured.RestAssured; import static io.restassured.module.jsv.JsonSchemaValidator.*; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.StringReader; public class SoapApiTest { @Test public void testAddOperation() { String soapRequestBody = “<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\” xmlns:tem=\”http://tempuri.org/\”>\n” + ” <soapenv:Header/>\n” + ” <soapenv:Body>\n” + ” <tem:Add>\n” + ” <tem:intA>10</tem:intA>\n” + ” <tem:intB>20</tem:intB>\n” + ” </tem:Add>\n” + ” </soapenv:Body>\n” + “</soapenv:Envelope>”; // Send the SOAP request using SAAJ and Rest Assured SOAPConnectionFactory soapConnectionFactory; SOAPConnection soapConnection = null; try { soapConnectionFactory = SOAPConnectionFactory.newInstance(); soapConnection = soapConnectionFactory.createConnection(); SOAPMessage soapRequest = MessageFactory.newInstance().createMessage(null, new StringReader(soapRequestBody)); SOAPMessage soapResponse = soapConnection.call(soapRequest, “http://www.dneonline.com/calculator.asmx”); // Extract the response body from the SOAP response TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); SOAPBody responseBody = soapResponse.getSOAPBody(); StreamSource source = new StreamSource(responseBody.extractContentAsDocument()); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); // Add your assertions here for validating the response // For example, you can use Rest Assured to validate the JSON response RestAssured.given().contentType(“application/json”).body(result.getWriter().toString()) .when().post(“/your-validation-endpoint”) .then().assertThat().body(matchesJsonSchemaInClasspath(“your-json-schema.json”)); } catch (Exception e) { e.printStackTrace(); } finally { if (soapConnection != null) { try { soapConnection.close(); } catch (Exception e) { e.printStackTrace(); } } } } } |
This code sends the SOAP request to the “Add” operation of the SOAP API, extracts the SOAP response, and then you can use Rest Assured to validate the JSON response against a JSON schema.
Conclusion
In conclusion, this tutorial has provided valuable insights into automating SOAP APIs using Rest Assured, backed by real-time examples. By following the steps outlined here, you can streamline your API testing process, ensuring efficiency and reliability. Rest Assured capabilities make it a valuable asset for testers in the quest for robust web service testing. Embrace these techniques to bolster your testing proficiency and deliver higher quality APIs. Start automating with confidence today.
Keep in mind that Rest Assured is primarily designed for RESTful API testing, so using it for SOAP APIs requires a bit of additional work with SAAJ. For pure SOAP testing, you may consider using dedicated SOAP testing libraries like Apache CXF or Spring WS.
Add comment