【发布时间】:2018-08-16 14:04:14
【问题描述】:
我有以下代码
public static void writeToOutputStream(byte[] bytesArr, OutputStream outputStream) {
try {
outputStream.write(bytesArr);
}
catch (IOException e) {
throw new NetModelStreamingException(
"IOException occurd during writing to stream. Error
Message:" + e.getMessage());
}
}
我想编写一个 JUnit 来测试我的代码是否会捕获 IOException。
PS:NetModelStreamingException 是扩展 RuntimeException 的自定义 Exception 类。
【问题讨论】:
-
好吧,创建一个扩展 OutputStream 并在调用其 write 方法时抛出 IOException 的类,将该 OutputStream 传递给您的方法,并检查您的方法是否抛出了您期望它抛出的内容。使用模拟框架会使这件事变得更容易一些,但并不多。
标签: java junit ioexception