首页 > 解决方案 > 如何在 Ant JunitLauncher 中将系统属性作为参数提供

问题描述

我正在尝试将我的测试套件从 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> 

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

标签: javaantjunit5

解决方案


Ant 1.10.4 确实支持 JUnit 5。但是,它并不支持 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 以将其清空后,您需要另一个脚本块。

推荐阅读