模拟|| JUnit测试createSOAPMessage方法

时间:2019-02-11 19:08:56

标签: java unit-testing soap junit

失去了测试我的createSOAPMessage方法的最佳方法/方法。基本上,该类应该做的是创建对我们的中央数据库的SOAP调用,并提取XML格式的某些数据。某些关键字已更改,以保护身份。

public class WebService {
        public SOAPMessage getData(String ticketNumber) throws Exception{
        //Create the SOAP Factory/Connections
        SOAPConnectionFactory soapConnectionFactory;
        SOAPConnection soapConnection;

        //Create SOAP Connection
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
        soapConnection = soapConnectionFactory.createConnection();

        //Send SOAP Message to SOAP Server
        String url = "http://blahblah.asmx";
        SOAPMessage soapResponse = soapConnection.call(createSOAPMessage(ticketNumber), url);
        soapConnection.close();

        return soapResponse;
    }



public static SOAPMessage createSOAPMessage(String ticketNumber) throws Exception {

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://blahblah";

        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPBody soapBody = envelope.getBody();
        SOAPHeader soapHeader = envelope.getHeader();

        envelope.removeNamespaceDeclaration(envelope.getPrefix());
        envelope.addNamespaceDeclaration("abc", serverURI);

        envelope.setPrefix("soapenv");
        soapHeader.setPrefix("soapenv");
        soapBody.setPrefix("soapenv");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("Content-Type", "text/xml; charset=utf-8");
        headers.addHeader("SOAPAction", "http://blahblah.net/ExtractXML");

        //This String xml will contain the relevant data I am pulling from the db with specific column names. I've removed them as well for protection.
        String xml = "<Title>" + "<Date>" + //you get the idea.




        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document doc = builder.parse(new InputSource(new StringReader(xml)));


        Element extractElement = doc.getDocumentElement();
        Element newDocumentElement = doc.createElementNS(serverURI, extractElement.getNodeName());

        // Set the desired namespace and prefix
        newDocumentElement.setPrefix("abc");

        Element xDoc = doc.createElement("xDoc");
        Element xDocElement = doc.createElementNS(serverURI, xDoc.getNodeName());
        xDocElement.setPrefix("abc");
        newDocumentElement.appendChild(xDocElement);

        // Copy all children
        NodeList list = extractElement.getChildNodes();
        while (list.getLength() != 0) {
            newDocumentElement.appendChild(list.item(0));
        }

        // Replace the original element
        doc.replaceChild(newDocumentElement, extractElement);

        soapBody.addDocument(doc);
        soapMessage.saveChanges();

        return soapMessage;

    }
}

由于我没有在类本身内调用getData方法,因此不必为该方法创建测试用例。这个想法只是JUnit测试这个类本身,并为createSOAPMessage方法创建测试用例。我只是不知道该怎么办。

0 个答案:

没有答案