问题描述
想在我的JUnit测试中寻求帮助。在我的方法中,我能够覆盖所有需要抛出IOException的部分。我对此做了很多变通方法,但似乎都没有。
这是单元测试中未涵盖IOException的方法
public String GetstrXMLValue(String strXML,String strCriteria) {
SAXBuilder builder = new SAXBuilder();
builder.setProperty(XMLConstants.ACCESS_S1,"");
builder.setProperty(XMLConstants.ACCESS_S2,"");
Document xmlDocument;
String strValue = "";
ErrorHandler objErrHdl = new ErrorHandler();
try {
InputStream objStream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
xmlDocument = builder.build(objStream);
XPathFactory xpath = XPathFactory.instance();
XPathExpression<Object> expr = xpath.compile(strCriteria);
List<Object> xPathSearchedNodes = expr.evaluate(xmlDocument);
if (xPathSearchedNodes.size() > 0) {
Content content = (Content) xPathSearchedNodes.get(0);
strValue = content.getValue().toString();
}
} catch (JDOMException e) {
objErrHdl.LogError(this.getClass().getName(),"GetstrXMLValue","ERROR RETRIEVING XML VALUES (XML=" + strXML + ";CRITERIA=" + strCriteria + ")",e.toString());
} catch (IOException e) {
objErrHdl.LogError(this.getClass().getName(),e.toString());
} catch (Exception e) {
objErrHdl.LogError(this.getClass().getName(),e.toString());
}
return strValue;
}
这是我的JUnit测试
@Test
public void testGetstrXMLValueThrowsIOException() throws IOException,ParserConfigurationException,SAXException,NoSuchMethodException,SecurityException,IllegalAccessException,IllegalArgumentException,InvocationTargetException,NoSuchFieldException {
OutputStream responseBody = new ByteArrayOutputStream();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
" <RESULT>\r\n" +
" <SHORT_NAME>XXXXXXXXXXXXX</SHORT_NAME>\r\n" +
" <ID_CODE>9999999999999</ID_CODE>\r\n" +
" </RESULT>";
xmlHandler.XMLGenerateRootResult();
xmlHandler.GetstrXMLValue(xml,".//RESULT/ID_CODE/text()");
try
{
InputStream input = XMLHandlerTest.readXML(xml);
byte[] buffer = new byte[1024];
int bytesRead;
input.close();
while ((bytesRead = input.read(buffer)) > 0)
{
responseBody.write(buffer,bytesRead);
throw new IOException();
}
}
catch(IOException e)
{
System.out.println(e);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)