I have built some code to ingest an XML document and parse through the values but now I'm stuck on testing the method.
What would be the proper way to unit run test on this method? I'm also unsure how to pass an xml document to run.
public class WebServiceTools
{
static public String getVersionFromWSResponseFromDOM(Document responseDocument) {
String versionDataAsXML = badData;
try {
responseDocument.normalizeDocument();
NodeList resultList = responseDocument.getElementsByTagName("ti:VersionResponse");
Element resultElement = (Element) resultList.item(0);
if (!badData.equalsIgnoreCase(resultElement.getTextContent())) {
versionDataAsXML = resultElement.getTextContent().trim();
}
} catch (Exception e) {
e.printStackTrace();
}
return versionDataAsXML;
}
}
package org.examples.tools;
import java.lang.reflect.Method;
public class ReflectApp {
public static void main(String[] args) {
//String parameter
Class[] paramString = new Class[1];
paramString[0] = String.class;
try{
//load the AppTest at runtime
Class cls = Class.forName("org.examples.tools.WebServiceTools");
Object obj = cls.newInstance();
//call the printItString method, pass a String param
method = cls.getDeclaredMethod("printItString", paramString);
method.invoke(obj, new String(" Do I put document here? "));
}catch(Exception ex){
ex.printStackTrace();
}
}
package org.examples.tools;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
public class TestGetVersion {
public static void main (String[] args) throws Exception {
String fileName = "C:/examples/VersionResponse.xml"; // Set path to file
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(fileName));
// Create do
String result = WebServiceTools.getVersionFromWSResponseFromDOM(doc);
// Treat result
System.out.print(result);
}
}