Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give System Properties as Parameters in Ant JunitLauncher

Tags:

java

junit5

ant

I am trying to migrate my test suite from Junit4 to Junit5. Have a bunch of System Properties given as parameters in the older targets which run tests on Junit4 but now as i am migrating to Junit5, JunitLauncher doesn't support this parameter .

Older Target which runs tests on Junit4:

<target name="test">
    <mkdir dir="${junit_reports.dir}" />
    <junit printsummary="${junit.printsummary}" haltonfailure="${junit.haltonfailure}" haltonerror="${junit.haltonerror}" showoutput="${junit.showoutput}" fork="true" forkmode="once" failureProperty="failed">
        <sysproperty key="clover.initstring" value="${clover.dbdir}/${clover.dbfile}" />
        <sysproperty key="rules.location" value="${classes.dir}/rules/impl" />
        <classpath>
            <path refid="classes.classpath" />
            <path refid="test.classpath" />
            <pathelement path="${basedir}/../../.." />
            <pathelement path="${test.classes.dir}" />
            <path location="${basedir}/../common/target/test_classes" />
            <pathelement location="${3rdparty.dir}/prime-server-framework/framework-core-mock.jar" />
        </classpath>
        <formatter type="${unittest.output.type}" />
        <batchtest fork="true" todir="${junit_reports.dir}">
            <fileset dir="${test.classes.dir}" includes="${tests.patternset}" />
        </batchtest>
    </junit>
</target>

New Target which runs tests on Junit5:

<target name = "sampletest">
    <mkdir dir="${junit_reports.dir}" />
    <junitlauncher>
        <classpath>
            <path refid="classes.classpath" />
            <path refid="test.classpath" />
            <pathelement path="${basedir}/../../.." />
            <pathelement path="${test.classes.dir}" />
            <path location="${basedir}/../common/target/test_classes" />
        </classpath>
        <!--<testclasses outputdir="${junit_reports.dir}">
          <fileset dir="${test.classes.dir}">
              <include name = "**/*Test.class"/>
          </fileset>
        </testclasses>-->
        <test name = "impl.RulesEngineValidationTest"/>
    </junitlauncher>
</target> 

How do i give system properties in new target?

like image 307
Dhruva Juloori Avatar asked Jun 30 '18 03:06

Dhruva Juloori


1 Answers

Ant 1.10.4 does support JUnit 5. However, it does not support all the features that Ant integration JUnit 4 does. In particular, it does not support forking the junit process and therefore passing system properties.

I found this question because I was trying to do the same thing. I found a workaround though. You can set the system properties in code before calling junitlauncher.

This code is what I used to set a single system property for file encoding. You could do something similar for your properties.

<script language="javascript">
  <![CDATA[
    var imports = new JavaImporter(java.lang.System);
    imports.System.setProperty('file.encoding', 'ISO8859_1')
  ]]>
</script>

Yours is a little more complicated since your properties use others. You can read an Ant variable from inside the code. (I don't know how to read one with a dot in the name so I got rid of the dot in this example)

<property name="cloverdbdir" value="clover-dir-property-value" />
<property name="cloverdbfile" value="clover-db-file-property-value" />

<script language="javascript">
  <![CDATA[
    var imports = new JavaImporter(java.lang.System);
    imports.System.setProperty('clover.initstring', cloverdbdir + '/' + cloverdbfile);
    print(imports.System.getProperty('clover.initstring'));
  ]]>
</script>

There are a few things to be aware of if you use this technique:

  1. Nashorn is deprecated for removal. It is definitely in Java 11. However, it isn't guaranteed to all future versions. It seems likely that Ant will add the system property functionality natively by then so I'm not worried about it.
  2. The system property remains set for the remainder of the build. This doesn't look like a problem for you. If it is, you'd need another script block after calling JUnit to null it out.
like image 161
Jeanne Boyarsky Avatar answered Oct 17 '22 04:10

Jeanne Boyarsky