In one of my JUnit tests, I am trying to load all the files contained in a directory. I used .getClassLoader().getResource("otherresources")
to find the directory. I then made a new java.io.File
. I then used listFiles()
to get all the children files and then used .getClassLoader().getResource()
again to load each of those files.
URL url = FileLoadTest.class.getClassLoader().getResource("otherresources");
File directory = new File(url.getPath());
File[] files = directory.listFiles();
Basically, I want to be able to load all the files in a directory without knowing exactly what they are.
I can properly run the test in Eclipse. When I go to build the project with Maven (mvn install
) or run the test case by itself using surefire (mvn -Dtest=FileTest test
) the test case fails with a NullPointerException
. I think the issue has something to do with the File api not working as intended within the JAR file that the resources are deployed to.
Any tips on how to fix this?