Java 如何测试需要SOAPHeader的SpringWS-webservice?

Java 如何测试需要SOAPHeader的SpringWS-webservice?,java,integration-testing,spring-ws,Java,Integration Testing,Spring Ws,我实现了一个SpringWS(2.0.2)服务,它需要soap头中的一些自定义元素。我试图使用Spring的MockWebServiceClient生成一个有效的请求来测试调度器、封送器等 我遇到的问题是MockWebSerivce似乎只支持Soap主体(负载) 我如何访问正在生成的soap请求以将正确的头放入其中 如果除了Spring的MockWebServiceClient之外,还有更好的库来实现这一点,那也不错 相关链接: 当我想用安全性测试SpringWeb服务时,我遇到了类似的问题,

我实现了一个SpringWS(2.0.2)服务,它需要soap头中的一些自定义元素。我试图使用Spring的MockWebServiceClient生成一个有效的请求来测试调度器、封送器等

我遇到的问题是MockWebSerivce似乎只支持Soap主体(负载)

我如何访问正在生成的soap请求以将正确的头放入其中

如果除了Spring的MockWebServiceClient之外,还有更好的库来实现这一点,那也不错

相关链接:

当我想用安全性测试SpringWeb服务时,我遇到了类似的问题,我最终使用spring拦截器在它们到达终点之前修改头部,我只为测试启用拦截器

创建一个拦截器,我实现了SmartEndpointInterceptor,如果您选择,您可以使用其他拦截器

public class ModifySoapHeaderInterceptor implements
    SmartEndpointInterceptor 
{
  //WSConstants.WSSE_NS;
   private static final String DEFAULT_SECURITY_URL = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    private static final String SECURITY_TAG = "Security";
    private static final String SECURITY_PREFIX = "wsse";
    private static final String USER_NAME_TOKEN = "UsernameToken";

    @Override
public boolean handleRequest(MessageContext messageContext, Object endpoint)
        throws Exception 
{
      SaajSoapMessage saajSoapMessage(SaajSoapMessage)messageContext.getRequest());
      SOAPHeader soapHeader = saajSoapMessage.getSaajMessage().getSOAPPart()
            .getEnvelope().getHeader();

       //you can modify header's as you choose
       Name headerElement = saajSoapMessage.getSaajMessage().getSOAPPart()
            .getEnvelope()
            .createName(SECURITY_TAG, SECURITY_PREFIX, DEFAULT_SECURITY_URL);
       SOAPHeaderElement soapHeaderElement = soapHeader
            .addHeaderElement(headerElement);
    SOAPElement usernameToken = soapHeaderElement.addChildElement(
            USER_NAME_TOKEN, SECURITY_PREFIX);

    SOAPElement userNameElement = usernameToken.addChildElement("Username",
            SECURITY_PREFIX);
    userNameElement.addTextNode("userid");//you can inject via spring

    SOAPElement passwordElement = usernameToken.addChildElement("Password",
            SECURITY_PREFIX);
    passwordElement.addTextNode("password");
       return true;
    }
}
在spring上下文中配置此拦截器

  <sws:interceptors>
     <bean class="prasanna.ws.security.wss4j.ModifySoapHeaderInterceptor"/>
  </sws:interceptors>


这将在消息到达终点之前向消息添加必要的安全头,您仍然可以使用MockWebServiceClient测试您的web服务。

我确实找到了smock库,它实现了我真正想要的功能:只需获取一个包含整个请求的文本文件


它支持与spring提供的东西相同的请求和响应匹配器。它还使我的测试非常独立。(而不是只在我的测试用例中使用的全新类。)

如您所述,
MockWebServiceClient
sendRequest()方法只使用给定的负载设置SOAP主体。它不接触SOAP标头

要设置SOAP头,您可以创建一个类,该类实现
RequestCreator
接口并设置SOAP头。将此类的实例传递给sendRequest()方法

例如:

类SoapActionCreator实现RequestCreator{
专用最终源有效载荷;
公共SoapActionCreator(源有效负载){
这个有效载荷=有效载荷;
}
@凌驾
公共WebServiceMessageCreateRequest(WebServiceMessageFactoryWebServiceMessageFactory)
抛出IOException{
WebServiceMessageWebServiceMessage=
新的PayloadMessageCreator(有效载荷).createMessage(webServiceMessageFactory);
SoapMessage SoapMessage=(SoapMessage)webServiceMessage;
SoapHeader=soapMessage.getSoapHeader();
//将Action元素添加到SOAP头中
StringSource headerSource=新StringSource(
"https://example.com/foo/bar");
Transformer Transformer=TransformerFactory.newInstance().newTransformer();
transform(headerSource,header.getResult());
返回webServiceMessage;
}
}

然后像这样使用
SoapActionCreator

    SoapActionCreator soapActionCreator = new SoapActionCreator(requestPayload);
    mockClient.sendRequest(soapActionCreator).
            andExpect(payload(responsePayload));

其中,
requestPayload
是SOAP请求主体,
responsePayload
是整个SOAP响应(头和主体)。

非常感谢。因为我使用一个备用context.xml进行测试,其中包括我的产品,所以我可以将拦截器放在我假设的上下文中。