我需要使用给定的SoapAction和Message测试Soap Server。
public class SoapActionCreator implements RequestCreator {
private final Source payload;
private final String soapAction;
public SoapActionCreator(Source payload, String soapAction) {
this.payload = payload;
this.soapAction = soapAction;
}
@Override
public WebServiceMessage createRequest(WebServiceMessageFactory webServiceMessageFactory) throws IOException {
WebServiceMessage webServiceMessage =
new SoapEnvelopeMessageCreator(payload).createMessage(webServiceMessageFactory);
SoapMessage soapMessage = (SoapMessage) webServiceMessage;
soapMessage.setSoapAction(soapAction);
return webServiceMessage;
}
}
在测试方法中:
mockClient = MockWebServiceClient.createClient(applicationContext);
Source requestPayload = new StringSource(
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
+ " <soap:Body>\r\n" + */" <Login xmlns=\"http://www.nightfreight.com/webservices\">\r\n"
+ " <username>local_user</username>\r\n"
+ " </Login>\r\n"
+ " </soap:Body>\r\n" + "</soap:Envelope>");
mockClient.sendRequest(new SoapActionCreator(requestPayload,
soapAction)).andExpect.....
由于有效载荷(方法调用中@RequestPayload标为param)为null,因此调用TestMethod时获取NullPointerException。 拦截时:messageContext.getRequest()。getPayloadSource()为“空”
但是在实际调用SoapService时: messageContext.getRequest()。getPayloadSource()是javax.xml.transform.dom.DOMSource@1237e0be
如果在删除消息的肥皂信封部分之后使用PayloadMessageCreator而不是SoapEnvelopeMessageCreator,它将再次起作用。我将需要具有自定义名称空间以及标头,因此PayloadMessageCreator在这种情况下将无用。