1

我正在使用 maven-surefire-plugin 版本 2.17 和 maven。

我正在使用 jacoco-maven-plugin 来分析我的 junit 测试:

我在 pom.xml 中设置的 jacoco 插件如下所示:

    <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.6.201602180812</version>
            <executions>
                <execution>
                    <id>agent-for-ut</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <append>true</append>
                    </configuration>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${sonar.jacoco.reportPath}</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我在 src/main/java/something/excel 下有一个类,如下所示:

package something.excel;

public class VDHTCStyle
{
    public int doSomething() {
        int i=0;
        return i+7;
    }
}

我在 src/test/java/something/excel 下的测试类如下所示:

package something.excel;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class VDHTCStyleStyleTest
{
    private VDHTCStyleStyle vDHTCStyleStyle;

    @Before
    public void setUp()
    {
        vDHTCStyleStyle = new VDHTCStyleStyle();
    }

    @Test
    public void shouldDoSomething() {
        int something = vDHTCStyleStyle.doSomething();
        assertEquals(something, 7);
    }

}

当我运行这个

mvn clean install

我在日志中看到了这一点:

[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ xxxxx ---
[INFO] Surefire report directory: /var/lib/jenkins/workspace/xxxx/modules/xx/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running something.excel.VDHTCStyleTest
|classnames | 2.2.3 | A simple utility for conditionally joining classNames together|
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec - in something.excel.VDHTCStyleTest 

现在,如果我查看生成的 jacoco-ut 报告文件,我会看到如下内容:

GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
Module: xxx,something.excel,VDHTCStyleStyle,9,0,0,0,3,0,2,0,2,0

据我了解,它认为没有涵盖任何行、指令、分支或方法。

为什么?帮助 :)

4

1 回答 1

2

问题似乎是 maven-surefire-plugin 在多模块项目中指定两次时的行为不可预测

我的父 pom 在构建部分有它,子模块 pom 在构建部分也有它。因此,它似乎正在运行测试,但子模块中的覆盖率始终为 0。

修复方法是从子模块 pom 中删除 maven-surefire-plugin,并且只将它放在父 pom 的构建部分中。

问题解决了。

于 2016-06-06T17:43:08.283 回答