![](/img/trans.png)
[英]how to read data from text file and pass them to junit test as parameterized test in java?
[英]Java: Can not read data file in JUnit test
我的測試包看起來像
test/
java/
com/
algos/
graphs/
GraphTest.java
resources/
graphs/
tinyG.txt
GraphTest
嘗試讀取tinyG
,如下所示
@Test
public void testTinyG() throws IOException {
final Graph g = new Graph(getBufferedReaderFor("tinyG.txt"));
System.out.println(g.toString());
}
private static BufferedReader getBufferedReaderFor(final String filename) {
return new BufferedReader(new InputStreamReader(GraphTest.class.getResourceAsStream("/graphs/" + filename)));
}
它失敗,並顯示NullPointerException
為
GraphTest.class.getResourceAsStream("/graphs/" + filename)
返回null
。
我在這里做錯了什么?
tinyG
數據如下
13
13
0 5
4 3
0 1
9 12
6 4
5 4
0 2
謝謝
Eclipse為項目運行JUnit測試時,通常會使用項目的工作目錄啟動測試。 因此,要從類路徑訪問tinyG.txt,必須使用路徑/test/resources/graphs/tinyG.txt
。 使tinyG.txt
起作用的唯一方法是使其與.class文件位於同一目錄中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.