问题描述
我想就如何在我的JUnit测试中捕获IOException寻求帮助,以测试发生错误后我的代码是否会抛出正确的异常。
这是我的主要代码:
public void LogHostStream(String v_strText) {
Date dtToday = new Date();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
String date = DATE_FORMAT.format(dtToday);
SimpleDateFormat FILE_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
String filedate = FILE_DATE_FORMAT.format(dtToday);
IConfiguration cfgHandler = ConfigurationFactory.getConfigHandle();
String logFileName = cfgHandler.GetXMLValue("config.xml","/config/log/host_logs");
String outText = String.format("%s \n %s \n",date,v_strText);
String fileName = logFileName + "_" + filedate + ".txt";
try (FileWriter fw = new FileWriter(fileName,true);
BufferedWriter bw = new BufferedWriter(fw);) {
bw.write(outText);
} catch (IOException e) {
e.printStackTrace();
}
这是我的JUnit:
@Test(expected = IOException.class)
public void testLogHostStreamThrowsIOException() throws IOException {
logger.LogHostStream("");
String logFileName = cfgHandler.GetXMLValue("ocomw_config.xml","/config/log/host_logs");
FileWriter fWriter = new FileWriter(logFileName,true);
BufferedWriter bw = new BufferedWriter(fWriter);
Assertions.assertThrows(IOException.class,() -> new BufferedWriter(bw));
}
我认为我必须在JUnit测试中创建错误的路径才能引发该异常,但是我无法获得所需的结果。我对此表示感谢。谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)