簡體   English   中英

Java:無法在JUnit測試中讀取數據文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM