开发者

Junit test for spring ws endpoint interceptor

Would appreciate any code examples of how to call a SpringWS endpoint intrceptor from a Junit test class. Particularly on how to prepare a SOAP message context and endpoint object. The SOAP message in the context will need to ha开发者_Go百科ve a custom SOAP header included.

Something like....

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

Any examples would be appreciated.


The MessageContext can be created by instantiating a DefaultMessageContext object. The request WebServiceMessage can created using the test support class PayloadMessageCreator, but this only appeared in Spring-WS 2.x.

The endpoint object can be anything - it depends what your interceptor does with it. If it doesn't actually use it, then you can just pass in null.


I had the same issue and was able to figure it out in part using @skaffman's suggestion.

Basically, I had a custom EndpointInterceptor that I wanted to test with real data so that I would know I had everything correct.

You will have to upgrade spring-ws-test and other spring-ws dependencies to version 2.0 or higher. I ended up using something different than 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 is the string value of an entire soap envelope.

Something similar to this:

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

You will obviously need to fill in your request payload, any namespaces, as well as your custom headers.

I set the endpoint object to null as I was not doing anything with it as part of my interceptor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜