5

I have a ant file that runs JUnits tests. These tests rely on a relative path to certain configuration files. I've tried to set the working directory for the batch test, but fail.

I want the working directory to be ${plugins.dir}/${name}

The JUnit part of the ant script:

<junit haltonfailure="no" printsummary="on" fork="true" showoutput="true" dir="${plugins.dir}/${name}">
    <jvmarg value="-Duser.dir=${plugins.dir}/${name}"/>
    <classpath> 
        <path refid="project.classpath"/>
        <pathelement location="${plugins.dir}/${dependency}/@dot/"/>
        <pathelement location="${plugins.dir}/${name}/" />
    </classpath>
    <formatter type="xml" />
    <sysproperty key="basedir" value="${plugins.dir}/${name}"/>
    <sysproperty key="dir" value="${plugins.dir}/${name}"/>
    <batchtest todir="${junit.output}">
        <fileset dir="${dir}"> 
            <include name="**\*AllTests.class" />
        </fileset>
    </batchtest>
</junit>

I've googled and searched but the workarounds I've found have been to set the "dir", "sysproperty" or "jvmarg". As you can see I've tried them all :)

Is there a way to print the current dir in the tag? It doesnt support . That would allow me to verify if the dir is actually changed and to what.

One wildcard in this equation is that this is being run in Hudson that starts upp an eclipse process that starts antrunner. This is so we can run both junit and eclipse plugin junit tests. Shouldn't have anything to do with the problem I think.

2
  • 3
    Is it not possible to simply fix your tests such that they are self-contained? E.g. if the resources you need are put somewhere in the classpath instead, you could load them through getClass().getResource(String) instead. Commented Dec 6, 2010 at 16:06
  • @Mark peters, Yes, that is possible. But that involves updating some 400-500 tests, so I'd prefer to consider that plan B.
    – Fredrik
    Commented Dec 7, 2010 at 14:13

3 Answers 3

5

I think you are right with setting the basedir property (see projects attributes). However, since it is a property of ANT (and not of the JVM) it is READ ONLY!

Does it effect other target if you set the basedir when calling your ant task? See Command Line reference.

ant -Dbasedir=somedir

Alternatively, span a new ant process to call your junit target. See the AntCall task or Ant task. Following examples assumes that the junit target contains your junit task. I used this task for other properties (never needed the basedir property so far.

<antcall target="junit">
  <param name="basedir" value="${plugins.dir}/${name}"/>
</antcall>

<ant dir="${plugins.dir}/${name}" target="junit" />
1
  • Thanks. I haven't has time to try this out yet, but it seems like it will solve atleast some of my problems so I'm marking this as the answer.
    – Fredrik
    Commented Dec 14, 2010 at 10:29
5

I had the same scenario and in my case I saw that dir="...." is ignored if run in same jvm so I simply added fork='true' and it worked.

Quote from Apache's documentation site "dir - The directory in which to invoke the VM. Ignored if fork is disabled.". More here.

1
  • and dont forget to use fork="true" on the batchtest part too, as it overwrites the junits fork property. That did the trick for me.
    – dag
    Commented Jul 24, 2014 at 11:33
1

I'm using NetBeans. When I add to Ant properties (from Tools, Options, Java, Ant) work.dir=C:/MyWorkingDir/ it executes ant with the following command and changes the working dir to C:\MyWorkingDir:

ant -f D:\\workspace\\lib\\project -Dfork=true -Djavac.includes=com/myapp/MyTest.java -Dtest.includes=com/myapp/MyTest.java "-Dwork.dir=C:/MyWorkingDir/" test-single

Not the answer you're looking for? Browse other questions tagged or ask your own question.