私のメソッドwriteList()
の完全なコードカバレッジを持っていますが、キャッチブロックのカバー方法はわかりません。キャッチブロックをカバーする方法は?
私の方法は次のとおりです。私はあなただったら
public class ListOfNumbers {
//...another code...
public void writeList() {
try (FileOutputStream inputStream = new FileOutputStream(this.pathOut);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream);
PrintWriter out = new PrintWriter(outputStreamWriter)) {
for (int i = 0; i < this.numbers.size(); i++) {
out.println("Value at: " + i + " = " + this.numbers.get(i));
}
} catch (final ArrayIndexOutOfBoundsException e) {
ListOfNumbers.LOGGER.error("Caught ArrayIndexOutOfBoundsException: " + e.getMessage(),
e);
} catch (final FileNotFoundException e) {
ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e);
} catch (final IOException e) {
ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e);
}
}
}
「カバー」とは何ですか?なぜ?してください。 –
まず、なぜArrayIndexOutOfBoundsExceptionをキャッチしますか?このような 'RuntimeException'をキャッチすることは一般的にひどい習慣であり、それは設計上の問題であることを示します –
[例外の単体テストを行う方法?](http://stackoverflow.com/questions/3305453/how-例外の対象となる単体テスト) – Tom