tags:

views:

3111

answers:

3

Hi,

My Ant build includes a junit task that runs some tests. In order for the tests to work, the value of the property that specifies the current working directory (user.dir) must be changed, but I am unsure how to achieve this.

The task in question currently looks like this:

<junit printsummary="withOutAndErr" fork="true" haltonfailure="yes" showoutput="true" filtertrace="false" dir="C:/workspace/obp-web">
 <jvmarg value="-Duser.dir=C:/workspace/obp-web"/>

 <classpath>
  <fileset dir="${web.lib.dir}" includes="**/*.jar"/>
  <fileset dir="${lib.dir}" includes="**/*.jar"/>
 </classpath> 
 <batchtest fork="no" todir="${web.build.dir}/testresults">
  <formatter type="xml"/>
  <zipfileset src="${web.build.dir}/test-obp-web.jar">
   <include name="**/*Test.class"/>
  </zipfileset>   
 </batchtest>
</junit>

Notice that I've tried to use both the "dir" attribute and the "jvmarg" task to change the working directory to C:/workspace/obp-web. However when I run Ant with verbose output turned on, I see the following output, which indicates that the working dir has not been set correctly:

[junit] dir attribute ignored if running in the same VM

[junit] Using System properties {java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=c:\jdk6\jre\bin, java.vm.version=10.0-b23, ant.lib rary.dir=C:\java\apache-ant-1.7.0\lib, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(T M) Client VM, file.encoding.pkg=sun.io, user.country=CA, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=Service Pack 1, java.vm.specification.name=Java Virtual Machine Specification, user.dir=c:\workspace\obp-ear, java.runtime.version=1.6.0_07-b06, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorse d.dirs=c:\jdk6\jre\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\Users\donal\AppData\Local\Temp\, line.separator=

Thanks in advance, Don

+3  A: 

Try using a jvmarg:

<junit>
  <jvmarg value="-Duser.dir=somedir"/>
  ...
</junit>

Note that fork must be true on both the junit tag and the batchtest tag as the batchtest tag overrides the value from junit. Jvmargs only work if junit forks a new JVM.

Kevin
I tried that, but no luck. I've updated the original item with the info.
Don
You cannot change the current working directory in Java by setting user.dir. Changing that system property is considered a user error.
David G
@David That is true but we are instructing junit to fork a new JVM with that user.dir property, not modifying the property in a running JVM
Kevin
+1  A: 

Use the attribute "dir" (must also fork the vm):

http://ant.apache.org/manual/OptionalTasks/junit.html

Jeffrey Fredrick
I tried that, but no luck. I've updated the original item with the info.
Don
It worked for me.
David G
A: 

Have you tried pathelement location? This worked for me.

  <classpath>
<!-- filesets, etc. -->
<pathelement location="C:/workspace/obp-web" />
  </classpath>