I am having some confusing behavior on a Maven2 unit test. The following test code produces different results running in Eclipse vs. Maven2:
File f = new File( JUnitConstants.TEST_FILE );
File root = new File( "." );
Log.info( "File Info:" );
Log.info( f.toString() + (f.exists() ? " exists" : " doesn't exist") );
Log.info( f.getAbsoluteFile().toString() + (f.getAbsoluteFile().exists() ? " exists" : " doesn't exist") );
Log.info( root.toString() + (root.exists() ? " exists" : " doesn't exist") );
Log.info( root.getAbsoluteFile().toString() + (root.getAbsoluteFile().exists() ? " exists" : " doesn't exist") );
In Eclipse, everything exists and the unit tests runs fine. In maven, the f.exists() method returns false; so it thinks that file doesn’t exist! Here is the output from the maven run test:
2013-01-10 09:50:51,737 [main] INFO - File Info:
2013-01-10 09:50:51,737 [main] INFO - target\test-classes\test\test.img doesn't exist
2013-01-10 09:50:51,737 [main] INFO - C:\Users\me\code\HEAD\modules\project\target\test-classes\test\test.img exists
2013-01-10 09:50:51,737 [main] INFO - . exists
2013-01-10 09:50:51,737 [main] INFO - C:\Users\me\code\HEAD\modules\project\. exists
So, the file exists, the root directory is what I expect, but why does Java think the file does not exist, when using relative paths?
I’m on Windows 7, 64-bit; using JDK 1.6_38 32-bit.
After some time, I’ve determined that the problem is the forking of the surefire test process. The maven build runs the tests under a command similar to:
I can run java with those exact parameters (inside the quotes) from the command line and everything works, but as soon as I fork them to another command window (the cmd.exe /X /C), it fails the same way.
I’ve solved this by disabling forking in the pom.xml file:
This still must be something in my system configuration, because other Windows 7 64-bit computers don’t have this issue, but turning off forking seems to be a solution.