Puce
Puce

Reputation: 38122

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?

Upvotes: 1

Views: 2249

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 1081

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));

Upvotes: 2

Related Questions