Showing posts with label Automation Testing with Java. Show all posts
Showing posts with label Automation Testing with Java. Show all posts

How to test a SOAP API by Rest Assured



What is SOAP API?

  • SOAP (Simple Object Access Protocol) is a protocol for exchanging structured data between systems over a network.
  • It uses XML as the message format and typically runs over HTTP/HTTPS.
  • SOAP APIs are often used in enterprise applications (like banking, telecom, insurance) because they enforce strict standards for security, structure, and messaging.

Key Features of SOAP API:
  • Message format → Always XML (not JSON).
  • Transport → Usually HTTP/HTTPS (but can also use SMTP, TCP, etc.).
  • WSDL (Web Services Description Language) → An XML document that describes the service (endpoints, operations, request/response structure).
  • Highly standardized → Supports security (WS-Security), transactions, and reliable messaging.

Testing a SOAP API GET request using Rest Assured is possible but requires understanding how SOAP works. However, SOAP typically uses POST, not GET, because it sends a full XML envelope in the body. But if the SOAP service is exposed via GET (rare, usually for testing or WSDL fetching), we can use RestAssured.get().


How to Test a SOAP API in Rest Assured

When using Rest Assured to test a SOAP API:

1. Understand the WSDL/Endpoint
  • Get the SOAP endpoint URL and know which operations are available.
  • Review the WSDL (it defines the request/response XML structure).
2. Prepare the SOAP Request Body (XML)
  • Create the SOAP envelope (the wrapper XML with <Envelope>, <Header>, and <Body> tags).
  • Add the operation and required parameters inside <Body>.
3. Send the request with Rest Assured
  • Use an HTTP method (usually POST) to send the SOAP envelope as the request body.
  • Set the appropriate headers (like Content-Type: text/xml or application/soap+xml).
4. Receive and parse the SOAP response
  • The server returns a SOAP response envelope in XML.
  • Extract values using XMLPath (similar to JSONPath but for XML).
5. Assertions/Validation
  • Verify status code (e.g., 200 OK).
  • Check that the SOAP response contains expected elements and values.










Key Concepts:

  • SOAP (Simple Object Access Protocol) is a protocol for exchanging structured XML data.

  • WSDL (Web Services Description Language) is used to describe SOAP services.

  • SOAP API is generally tested using POST, but for some services (like fetching WSDL), GET is used.


Example SOAP GET Use Case:

Fetch the WSDL file or perform a test GET to an endpoint.

Let’s use this public SOAP service for currency conversion WSDL (GET example):
http://www.dneonline.com/calculator.asmx?WSDL

This WSDL can be fetched using a GET request.











Step-by-step: Testing SOAP GET using Rest Assured:


Maven Dependency:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>



Java code:

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class SoapGetTest {
    public static void main(String[] args) {
        // Base URI for the SOAP service
        String url = "http://www.dneonline.com/calculator.asmx?WSDL";

        // Send GET request
        Response response = RestAssured
                .given()
                .when()
                .get(url)
                .then()
                .extract()
                .response();

        // Print status code and response
        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Content-Type: " + response.getContentType());
        System.out.println("Response Body:\n" + response.getBody().asString());
    }
}


Code explanation:

  • Set the base URI
  • Send GET request
  • Print status code and response on console


We can assert response also by below code.

Optional: Add Test Assertion

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class SoapGetTest {
    public static void main(String[] args) {
        given()
        .when()
            .get("http://www.dneonline.com/calculator.asmx?WSDL")
        .then()
            .statusCode(200)
            .contentType("text/xml; charset=utf-8")
            .body(containsString("definitions"));
    }
}


Suggested Posts:

1. Validate Keys of API in RestAssured
2. Test Form Authentication in RestAssured
3. Test DELETE API in RestAssured
4. First RestAssured Code to Test API
5. Validate API Response from Database in RestAssured