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

Dhr*_*ori 7 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>
Run Code Online (Sandbox Code Playgroud)

在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> 
Run Code Online (Sandbox Code Playgroud)

如何在新目标中提供系统属性?

Jea*_*sky 1

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>
Run Code Online (Sandbox Code Playgroud)

你的情况有点复杂,因为你的属性使用其他属性。您可以从代码内部读取 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>
Run Code Online (Sandbox Code Playgroud)

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

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