java - 如何在 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>
如何在新目标中赋予系统属性?
解决方案
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>
如果您使用此技术,需要注意以下几点:
- Nashorn 已被弃用以移除。它肯定在 Java 11 中。但是,不能保证所有未来的版本都可以使用。到那时,Ant 似乎很可能会在本地添加系统属性功能,所以我并不担心。
- 系统属性在构建的其余部分保持设置。这对您来说似乎不是问题。如果是,则在调用 JUnit 以将其清空后,您需要另一个脚本块。
推荐阅读
- java - JBoss 7.1.0.EAP 无法加载 LogManager
- c - 为什么我的代码不要求用户输入?
- javascript - React-navigation:如何根据条件为屏幕设置初始参数?
- android - 尝试在android应用程序中连接到服务器时,如何设置视图首先出现?
- python - 将 Dash 应用程序部署到 Heroku:错误代码=H10、H13
- facebook-opengraph - Whatsapp 不适用于开放图形协议
- reactjs - 在 ReactJS 中调用挂载函数
- php - 有没有办法在 HTML 文件(扩展名)中运行 for 循环?
- css - CSS 过渡计时函数和动画名称
- python - 从 BeautifulSoup 获取类数据