As part of a JUnit test (executed from ANT) I need access to a fair few properties that are not only environment dependent, but can also change from one run to the next. In order to not affect any of numerous other devs that might use the same build.xml I wanted to define these properties on the command line with the -D flag (they’re easily populated using a script). Reading about the <junit> task in ant I thought that these properties would be easily duplicated to the VM running the JUnit tests using the clonevm flag, but this doesn’t seem to work. A simple <echo> before the <junit> call confirms the properties are set correctly, and I then execute the JUnit tests with the following:
<junit
printsummary="on" showoutput="true" timeout="${tests.timeout.value}"
haltonfailure="false" errorproperty="test.error.occurred"
haltonerror="false" failureproperty="test.failure.occurred" forkmode="perTest"
maxmemory="${project.junit.maxmemory}" clonevm="true">
<jvmarg line="${project.test.vmargs}"/>
<sysproperty key="java.io.tmpdir" value="${java.io.tmpdir}"/>
<formatter type="xml" usefile="true"/>
<classpath>
<path refid="build.test.classpath"/>
</classpath>
<batchtest fork="yes" todir="${test.rpt.dir}">
<fileset dir="${test.bin.dir}">
<include name="**/${test.classes.run}.class"/>
<includesfile name="${test.run.includesfile}"/>
<!-- ignore inner classes -->
<exclude name="**/*$*.class"/>
</fileset>
</batchtest>
</junit>
I then attempt to read a property value using System.getProperty("property"); but always get null. I assume I must be doing something wrong so that the properties aren’t sent to the JUnit VM, or I’m not reading it correctly. Any ideas? I guess I could work around the problem (use the script to write a temporary properties file, then read that from the test), but I’d still like to know how to do this if it’s even possible.
Try using a nested syspropertyset element.
See junit documentation