如何在Ant JunitLauncher中提供系统属性作为参数

时间:2018-06-30 03:28:34

标签: java ant junit5

我正在尝试将测试套件从Junit4迁移到Junit5。在较旧的目标中有大量的系统属性作为参数提供,这些目标在Junit4上运行测试,但是现在当我迁移到Junit5时,JunitLauncher不支持此参数。

在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>

在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> 

如何在新目标中赋予系统属性?

1 个答案:

答案 0 :(得分:1)

Ant 1.10.4确实支持JUnit5。但是,它不支持Ant集成JUnit 4所具有的所有功能。特别是,它不支持分叉junit进程,因此不支持传递系统属性。

我发现了这个问题,因为我试图做同样的事情。我找到了解决方法。您可以在调用junitlauncher之前在代码中设置系统属性。

这段代码是我用来设置文件编码的单个系统属性的代码。您可以对属性进行类似的操作。

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

您的房屋要复杂一些,因为您的房屋使用其他房屋。您可以从代码内部读取Ant变量。 (我不知道如何读取名称中带点的字母,因此在此示例中我删除了该点)

<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>

如果使用此技术,需要注意以下几点:

  1. 不建议将Nashorn除去。它肯定是在Java 11中。但是,并不能保证所有将来的版本。到那时,Ant似乎很有可能会在本地添加系统属性功能,因此我对此并不担心。
  2. 系统属性将为其余构建设置。这对您来说似乎不成问题。如果是这样,则在调用JUnit将其清空后,您将需要另一个脚本块。