Search code examples
javaspring-ws

spring-ws-test: How to mock a SoapFaultDetail


With spring-ws-test how can I mock a SoapFaultDetail to return the expected error payload?

ResponseCreators seems to support only faultString/ faultReason:

mockServer.expect(anything()).andRespond(withClientOrSenderFault(faultStringOrReason, Locale.GERMAN));

The detail element is not set. I need the fault to contain my custom payload however.

Is there a high-level API to do this?


Solution

  • You can use the withPayload method of ResponseCreators and provide it with a SOAP fault that contains your custom payload as shown below:

        Source faultPayload = new StringSource(
                "<SOAP-ENV:Fault xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://domain/schema/common/fault\">"
                        + "<faultcode>SOAP-ENV:Server</faultcode>"
                        + "<faultstring xml:lang=\"en\">fault</faultstring>"
                        + "<detail>"
                        + "<v1:custompayload>"
                        + "<v1:element1>element1</v1:element1>"
                        + "<v1:element2>element2</v1:element2>"
                        + "</v1:custompayload>" 
                        + "</detail>"
                        + "</SOAP-ENV:Fault>");
    
        mockWebServiceServer.expect(payload(requestPayload))
                .andRespond(withPayload(faultPayload));