Junit test for spring ws endpoint interceptor

I would appreciate any code examples on how to invoke the SpringWS endpoint intraceptor from the Junit test class. In particular, how to prepare the SOAP message context and endpoint object. In a SOAP message, a custom SOAP header must be included in the context.

Sort of....

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.

+4
source share
2 answers

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

The endpoint object can be anything - it depends on what your interceptor does. If it doesn't actually use it, you can just go to null .

+1
source

I had the same problem and I was able to find out partly with the @skaffman suggestion.

Basically, I had a custom EndpointInterceptor that I wanted to test using real data so that I knew that everything was correct.

You will need to update spring-ws-test and other spring-ws dependencies to version 2.0 or higher. I ended up using something other 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 the entire soap conversion.

Something like 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 out your request payload, any namespaces, as well as your custom headers.

I set the endpoint object to zero because I did not do anything with it as part of my interceptor.

0
source

Source: https://habr.com/ru/post/1345693/


All Articles