setTrailer()
ErParser クラス内のメソッドのテスト ケースを作成しようとしています。setTrailer()
には try-catch 句があり、その catch 句の 1 つで catches しNullPointerException
ます。setTrailer()
をスローしてキャッチする場合の Junit テストを作成しようとしていますNullPointerException
が、テスト ケースは失敗し続けます。メソッド自体で既に例外をキャッチしたためですか?代わりに、テスト ケースで例外をキャッチする必要がありますか?
テストケース:
public class TestERParser {
@Test(expected=NullPointerException.class)
public void nullSetTrailer() {
ERParser recCurrParse = new ERParser();
recCurrParse.setTrailer(null);
}
}
ERParser クラス内の setTrailer() メソッド:
public class ERParser {
private static final String TRAILER_E = "GRAND TOTAL";
private static final String TRAILER_R = "TRAILER";
public String trailerRecord;
/**
* Constructs an ERParser object.
*/
public ERParser() {
this.trailerRecord = null;
this.trailerVals = null;
}
/**
* Populates the trailerRecord field with the summary (trailer) record of the input file.
* @param file Input file
* @throws NullPointerException, FileNotFoundException, IOException
*/
public void setTrailer(File file) {
try {
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader (fReader);
String currLine = new String();
readLoop:
while (bReader.ready()) {
currLine = bReader.readLine();
if (currLine.contains(TRAILER_E) || currLine.contains(TRAILER_R)) {
break readLoop;
}
}
this.trailerRecord = currLine.trim();
System.out.println("From setTrailer(): " + this.trailerRecord);
fReader.close();
bReader.close();
} catch (NullPointerException exception) {
exception.printStackTrace();
} catch (FileNotFoundException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}