If you need any other information please let me know.
Short Version:
This line of code causes a NullPointerException when tested with jUnit 4.12:
File[] files = new File(searchPath).listFiles();
Long version:
First of all, this is the enviroment:
The exception only occurs when i run jUnit-tests. When i build it without any tests, everything works fine and the function listFiles() from File[] files = new File(searchPath).listFiles() doesn't return null.
gradle clean & gradle build -x test & gradle rest:jettyRun 2> test.txt
This is the problem-function, called in the constructor of my class:
/**
* Scans for JSON files on given path
* @param searchPath full path given
* @return String[] with the full paths and names of found files
*/
private String[] scanForJsons(String searchPath){
System.out.println("search path "+searchPath); // shows D:\..\core\files
File[] files = new File(searchPath).listFiles(); // problem line, allways returns null
System.out.print(((files!=null)?"Not null" : "Null"));
System.out.println("files.length: "+files.length);
String[] stringArray = new String[files.length];
for(int i = 0; i < files.length; i++){
String path = files[i].getName();
if (path.endsWith(".json")) {
path = path.substring(0, path.length() - 5);
}
stringArray[i] = path;
}
return stringArray;
}
The documentation for File.listFiles()
includes:
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
So it looks like that's what's happening. A path of d:\..\core\files
doesn't look like a valid pathname to me... how can you expect a directory to be the parent of a root directory?
You need to change your test to provide a valid path - and quite possibly change your method to cope with listFiles()
potentially returning null
.
See more on this question at Stackoverflow