Reputation: 91949
my test package looks like
test/
java/
com/
algos/
graphs/
GraphTest.java
resources/
graphs/
tinyG.txt
The GraphTest
tried to read tinyG
as follows
@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)));
}
It fails with NullPointerException
as
GraphTest.class.getResourceAsStream("/graphs/" + filename)
returns null
.
What is that I am doing wrong here?
tinyG
has data like
13
13
0 5
4 3
0 1
9 12
6 4
5 4
0 2
Thank you
Upvotes: 1
Views: 1584
Reputation: 2020
When Eclipse runs JUnit tests for a project, it normally starts the tests using the working directory of the project. So, to access tinyG.txt from the classpath, you would have to use the path /test/resources/graphs/tinyG.txt
. The only way to have tinyG.txt
work would for it to reside in the same directory as the .class file.
Upvotes: 2