Ant和Jacoco:需要将junit的fork设置为“true”,而不是将属性设置为true

时间:2012-08-20 18:28:48

标签: ant junit jacoco

我正在使用Jacoco Junit任务并尝试以下方法:

    <!-- run the junit tests -->
    <echo>DEBUG: $${junit.fork} = "${junit.fork}"</echo>
    <jacoco:coverage
        destfile="${target.dir}/jacoco.exec"
        append="false">
        <junit fork="${junit.fork}" includeAntRuntime="true">
            <classpath>
                <pathelement path="${main.destdir}"/>
                <pathelement path="${test.destdir}"/>
            </classpath>
            <classpath refid="test.classpath"/>
            <formatter type="${junit.formatter.type}"/>
            <batchtest      todir="${junit.batchtest.todir}">
                <fileset dir="${test.destdir}" />
            </batchtest>
        </junit>
    </jacoco:coverage>

得到以下内容:

test:
     [echo] DEBUG: ${junit.fork} = "true"
[jacoco:coverage] Enhancing junit with coverage

BUILD FAILED
D:\build.xml:233: Coverage can only be applied on a forked VM

Total time: 6 seconds

D:\>

如您所见,${junit.fork}属性设置为true,我在<junit fork="${junit.fork}"/>中使用了该属性。

但是,我只是设置<junit fork="true">而不是使用该属性,它可以正常工作:         

    <jacoco:coverage
        destfile="${target.dir}/jacoco.exec"
        append="false">
        <junit fork="true" includeAntRuntime="true">
            <classpath>
                <pathelement path="${main.destdir}"/>
                <pathelement path="${test.destdir}"/>
            </classpath>
            <classpath refid="test.classpath"/>
            <formatter type="${junit.formatter.type}"/>
            <batchtest      todir="${junit.batchtest.todir}">
                <fileset dir="${test.destdir}" />
            </batchtest>
        </junit>
    </jacoco:coverage>

当我使用ant -d test属性时,我已运行${junit.fork}来验证Java JVM是否正在分支。

为什么JaCoCo坚持认为除非我将fork参数设置为字符串true而不是设置为等于true的属性,否则JUnit测试不会被分叉?

1 个答案:

答案 0 :(得分:3)

根据Jacoco coverage task source code

...
public void enhanceTask(final Task task) {
    final RuntimeConfigurable configurableWrapper = task.getRuntimeConfigurableWrapper();

    final String forkValue = (String) configurableWrapper.getAttributeMap().get("fork");
    if (!Project.toBoolean(forkValue)) {
        throw new BuildException("Coverage can only be applied on a forked VM", getLocation());
    }
    addJvmArgs(task);
}
...

从configurableWrapper读取forkValue。使用由${junit.fork}调用的方法RuntimeConfigurable.maybeConfigure来评估属性映射值(如Task.mayBeConfigure)。当你继续查看Jacoco源代码时:

...
public void addTask(final Task task) {
    if (childTask != null) {
        throw new BuildException("Only one child task can be supplied to the coverge task", getLocation());
    }
    this.childTask = task;
    final String subTaskTypeName = task.getTaskType();
    final TaskEnhancer enhancer = findEnhancerForTask(subTaskTypeName);
    if (enhancer == null) {
        throw new BuildException(format("%s is not a valid child of the coverage task", subTaskTypeName), getLocation());
}
    if (isEnabled()) {
        log(format("Enhancing %s with coverage", childTask.getTaskName()));
        enhancer.enhanceTask(task);
    }
    task.maybeConfigure();
}
...

在增强任务(mayBeConfigure)后调用此方法(enhancer.enhanceTask(task);)。 我想这是你的问题和一个bug ......我们应该把它报告给jacoco bug跟踪器。

Jacoco issue