Issue
I have a project that has a resource file in test folder:
- src/test/resources/myfolder/testfile.txt
I have:
@Test
public void test() {
String args[] = { "myfolder/testfile.txt" };
MyClass.load(args);
}
And this is MyClass.java method:
public void load(String filePath)
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classloader.getResourceAsStream(filePath);
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
reader = new BufferedReader(streamReader);
//...
}
If I launch the tests from Eclipse, all the tests goes well.
I I launch the maven clean install, test fails with java.lang.NullPointerException
at this line:
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
What I have to do?
Thanks
Solution
Your testfile.txt
resource is in the right place. This should work unless you have custom Maven resource filtering rules e.g. to exclude .txt
files. Check what's in the target/test-classes
after the failed build.
You could try to use absolute resource path /myfolder/testfile.txt
instead and stop using ContextClassLoader
:
String path = "/myfolder/testfile.txt";
InputStream inputStream = MyClass.class.getResourceAsStream(path);
Answered By - Karol Dowbecki