问题
I want to unit-test the mapper objects that map/translate web service types generated by wsimport in to my own domain objects. I also want to test error-scenarios, such as SOAP faults and such, and I am thinking it would be best to test the mapper objects on authentic SOAP responses. I do not want to fire requests to the web service itself as this requires access to the web service, and poses round-trip time for each test.
Given this scenario, I am seeking to unmarshal SOAP messages from a particular XML-file containing a SOAP Envelope. I want to unmarshal the SOAP Envelope, and in turn the payload in the body to the corresponding Java types.
I've managed to unmarshal the payload itself by using JAXB unmarshalling, but I have not found a way to allow me to handle SOAP responses with SOAP faults an similar.
Is there an approach that given a SOAP Envelope XML-file would allow me to test my mappers in an easy manner?
回答1:
Have you tried standard java SOAP API (javax.xml.soap)?
Something like this:
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage message = mf.createMessage();
SOAPPart soapPart = message.getSOAPPart();
FileInputStream is = new FileInputStream(file);
soapPart.setContent(new StreamSource(is));
来源:https://stackoverflow.com/questions/3768898/unmarshalling-soap-envelope-from-file-in-java