12
votes

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=

4

4 Answers

15
votes
7
votes

Try using a jvmarg:

<junit fork="yes">
  <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.

1
votes

Same problem as you.

I resolved it by making the batchtest fork to true :

batchtest fork="no" ..

to

batchtest fork="yes" ..

0
votes

Have you tried pathelement location? This worked for me.

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