Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the working directory for the Ant 'junit' task?

Tags:

java

junit

ant

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=

like image 403
Dónal Avatar asked Jan 21 '09 20:01

Dónal


People also ask

Which task you will call to run tests in ant?

To run unit tests with Ant from the command line: Open a command line window. Go to the directory where you have generated the code. Run the JUnit test task: Ant junit.

What does * stands for in * Test Java in ant?

**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"


2 Answers

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

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

like image 122
Jeffrey Fredrick Avatar answered Oct 22 '22 19:10

Jeffrey Fredrick


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.

like image 21
Kevin Avatar answered Oct 22 '22 21:10

Kevin