JaCoCo 缺少可执行文件问题

JaCoCo missing executable issue

我有一个小型 Maven 应用程序,它使用 JaCoCo 来生成测试覆盖率报告。我不断收到以下错误:

Skipping JaCoCo execution due to missing execution data file

这是我的 POM.xml 文件。我自己删除了项目元素。

<modelVersion>4.0.0</modelVersion>

<groupId>de.mathema.www</groupId>
<artifactId>jacoco_sample_app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jacoco_sample_app</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <plugins>

    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.6.201602180812</version>
      <executions>
        <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>           
  </plugins>
</build>

这是我的主要 class,它位于 src/main/java 结构下:

public class Punkt {
    private Double x;
    private Double y;

    public Punkt(Double x,Double y) {
        this.x = x;
        this.y = y;
    }

    public Punkt(Punkt zweiterPunkt) {
        this.x = zweiterPunkt.x;
        this.y = zweiterPunkt.y;
    }

    public Double getX() {
        return x;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public Double getY() {
        return y;
    }

    public void setY(Double y) {
        this.y = y;
    }

    public boolean punkteVergleichen(Punkt zweiterPunkt) {
        return this.getX().equals(zweiterPunkt.getX()) &&  
       this.getY().equals(zweiterPunkt.getY());

    }
    public String toString() {
        return "("+this.x+","+this.y+")";
    }

}

这里是 class,单元测试在 src/test/java 结构下:

public class PunktUnitTests {

    @Test
    public void PunkteVergleichen()  throws Exception {
        //a(10.0, 20.0)
        Punkt a = new Punkt(10.0,20.0);

        //b(30.0,40.0)
        Punkt b = new Punkt(30.0,40.0);

        //Der Fall X1!=X2 und Y1!=Y2
        //a(10.0,20.0) und b(30.0,40.0) sind unterschiedlich
        assertFalse(a.punkteVergleichen(b));

        //Der Fall X1 == X2 && Y1 == Y2
        //a(10.0,20.0) und c(10.0,20.0) sind gleich
        Punkt c = new Punkt(a);
        assertTrue(a.punkteVergleichen(a));

        //Der Fall X1 == X2 && Y1 != Y2
        //a(10.0,20.0) und c(10.0,90.0) sind unterschiedlich
        c.setX(10.0);c.setY(90.0);

        assertFalse(a.punkteVergleichen(c));
    }
}

我该如何解决这个问题?

虽然消息可能有点误导,但 JaCoCo Maven plugin will emit this warning when no tests have been run. This is your problem here: by default, the Maven Surefire Plugin 执行遵循特定命名约定的测试,即:

  • "**/Test*.java";
  • "**/*Test.java";
  • "**/*TestCase.java".

但是您的测试名为 PunktUnitTest<b>s</b>,尾随 "s",这导致它无法执行.因此,会发生以下情况:Surefire 插件未 运行 进行任何测试,因此从未使用 JaCoCo 代理,因此未创建执行数据文件。

这里的简单解决方案是根据 Surefire 插件的约定重命名您的测试,即 PunktUnitTest.

另一种解决方案是将插件配置为执行以 "s":

结尾的测试
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <includes>
      <include>**/*Tests.java</include>
    </includes>
  </configuration>
</plugin>

请注意,如果您想在将来防止此类错误,您可以将 Surefire 插件配置为在没有测试 运行 时失败,方法是将 failIfNoTests 参数设置为 true.