Mocking XML & SOAP APIs (Legacy & Modern)

Learn how to record and replay XML/SOAP APIs in Spring Boot integration tests. Handle namespaces, XPath matching, and mixed JSON/XML workflows automatically.

While the world has moved to REST and GraphQL, many enterprise systems still rely on XML and SOAP. Testing these services is notoriously painful due to:

  • Namespaces: <soapenv:Body> vs <ns1:Body> prefix hell.
  • Strict Formatting: Whitespace and ordering issues breaking mocks.
  • Dynamic Fields: Timestamps and nonces inside deep XML structures.

StableMock handles XML as a first-class citizen, solving these pain points automatically.

Handling Namespaces Automatically

One of the most common issues with XML mocking is namespace prefixes changing between environments (e.g., dev uses ns1:, prod uses soap:).

StableMock solves this by generating namespace-agnostic XPath selectors when it detects dynamic fields. Instead of matching /ns1:Request/ns1:ID, it matches /*[local-name()='Request']/*[local-name()='ID'].

Example: Testing a SOAP Service

Here is a test that calls a SOAP-like XML endpoint. Notice how we don't need any complex XML configuration.

@U(urls = { "https://postman-echo.com" })
@SpringBootTest
class XmlNamespaceTest extends BaseStableMockTest {

    @Test
    void testSoapRequest() {
        // Send a request with a specific namespace prefix (ns4)
        String xml = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                     "  <soap:Body>" +
                     "    <ns4:GetStockPrice>" +
                     "      <ns4:Price>123.45</ns4:Price>" +
                     "    </ns4:GetStockPrice>" +
                     "  </soap:Body>" +
                     "</soap:Envelope>";

        // StableMock matches this correctly, even if the namespace prefix definitions change slightly,
        // as long as the structure is valid.
        client.sendXml(xml);
    }
}

💡 Deep Dive: See how we verify namespace handling in XmlNamespacePrefixTest.java.

Mixed Protocols (JSON + XML)

Modernization projects often require calling a new JSON API and a legacy XML service in the same transaction. StableMock handles this seamlessly with multi-URL support.

@U(
    urls = {
        "https://api.legacy-corp.com", // Returns XML
        "https://api.modern-stripe.com"  // Returns JSON
    }
)
@SpringBootTest
class MigrationTest {
    // ... test logic calling both services ...
}

When you define multiple URLs, StableMock generates a separate mapping folder for each one (annotation_0, annotation_1), ensuring no collisions between your XML stubs and JSON stubs.

Next: Learn how to debug recordings with Reports & Generation.

Stop Rewriting Mocks By Hand, Son

Record once. Test offline forever.

Join developers who've made the switch. Free forever. No credit card required.

🌾 MIT License | 🚀 Open Source | 💯 Free Forever