【问题标题】:Junit test for spring ws endpoint interceptorspring ws端点拦截器的Junit测试
【发布时间】:2011-03-29 11:01:12
【问题描述】:

如果有任何关于如何从 Junit 测试类调用 SpringWS 端点 intrceptor 的代码示例,我将不胜感激。特别是关于如何准备 SOAP 消息上下文和端点对象。上下文中的 SOAP 消息需要包含自定义 SOAP 标头。

类似......

public class MyInterceptorTest

private static String "... my XML SOAP test message ...";

@Test
public testMyInterceptor() {
    myMessageContext = ... Build a MessageContext with the XML message string;
    myEndPointObject = ... Build an endpoint object;
    boolean result = MyInterceptorClass.handleRequest(myMessageContext, myEndPointObject);
    ... Check results;
}

任何示例都将不胜感激。

【问题讨论】:

  • 使用 Spring-WS 2.0,需要创建一个带有自定义 SOAP 标头的 SOAP 消息。寻找一个如何构建一个 MessageContext 的例子。

标签: junit spring-ws


【解决方案1】:

MessageContext 可以通过实例化 DefaultMessageContext 对象来创建。可以使用测试支持类PayloadMessageCreator 创建请求WebServiceMessage,但这仅出现在Spring-WS 2.x 中。

端点对象可以是任何东西——这取决于你的拦截器用它做什么。如果它实际上并没有使用它,那么你可以直接传入null

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并且能够通过@skaffman 的建议部分解决。

    基本上,我有一个自定义 EndpointInterceptor,我想用真实数据进行测试,这样我就知道我做的一切都是正确的。

    您必须将 spring-ws-test 和其他 spring-ws 依赖项升级到 2.0 或更高版本。我最终使用了不同于 PayloadMessageCreator 的东西。

    final Source payload = new StreamSource(new StringReader(soapPayload));
    SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());
    WebServiceMessage requestPayload = new SoapEnvelopeMessageCreator(payload).createMessage(saajSoapMessageFactory);
    MessageContext messageContext = new DefaultMessageContext(requestPayload, saajSoapMessageFactory);
    

    soapPayload 是整个肥皂信封的字符串值。

    类似的东西:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header>
            ...fill in your custom headers here
        </soapenv:Header>
        <soapenv:Body><someRequest>...</someRequest></soapenv:Body>
    </soapenv:Envelope>
    

    您显然需要填写您的请求负载、任何命名空间以及您的自定义标头。

    我将端点对象设置为 null,因为我没有对它做任何事情作为我的拦截器的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2012-08-03
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      相关资源
      最近更新 更多