I have a junit testcase and a file that I want to load during that test. The file is placed under src/main/resources
, whereas the packages are named same as the java testfile:
src/test/java/my/path/to/FileTest.java
src/test/resource/my/path/to/test.txt
Usage:
public class FileTest {
@Test
public void testRead() {
String content = IOUtils.toString(this.getClass().getResourceAsStream("test.txt"), Charset.defaultCharset());
System.out.println(content);
}
}
Exception when running the testcase in IDE:
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2272)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:1041)
What might be wrong here?