2016-11-25 73 views
1

使用spring-ws-test如何模擬SoapFaultDetail以返回預期的錯誤負載?spring-ws-test:如何模擬SoapFaultDetail

ResponseCreators似乎只支持faultString/faultReason:

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

細節元素未設置。然而,我需要故障來包含我的自定義有效載荷。

有沒有一個高級API來做到這一點?

回答

2

您可以使用ResponseCreatorswithPayload方法,並提供其與包含自定義有效載荷的SOAP錯誤,如下圖所示:

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

這似乎設置信封作爲有效載荷,這是不對的。 – Puce

+0

withSoapEnvelope可以使用,雖然 – Puce

+0

你能解釋爲什麼上述錯誤? 'withPayload'不會將信封設置爲有效內容,它將給定的資源XML設置爲有效內容響應(SOAP主體元素的內容)。在出現SOAP錯誤的情況下,這將是SOAP信封名稱空間中的SOAP Fault元素(如上所示)。在我的單元測試用例中,上面觸發了一個SoapFaultClientException,我能夠獲取FaultDetail並將自定義內容解組到正確的對象。 – CodeNotFound