2

Jacoco reports for this project contain line coverage for test classes, despite attempting to exclude tests as below

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.3</version>
            <configuration>
                <excludes>
                    <exclude>**/*Test.*</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <configuration>
                        <excludes>
                            <exclude>**/*Test.*</exclude>
                        </excludes>
                    </configuration>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <configuration>
                        <excludes>
                            <exclude>**/*Test.*</exclude>
                        </excludes>
                    </configuration>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule implementation="org.jacoco.maven.RuleConfiguration">
                                <excludes>
                                    <exclude>**/*Test.*</exclude>
                                </excludes>
                                <element>CLASS</element>
                                <limits>
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.94</minimum>
                                    </limit>
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.85</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>

The above pom is basically us shotgunning the config with solutions from existing StackOverflow questions, but none of them are working.

Any feedback would be much appreciated!

fred
  • 1,812
  • 3
  • 37
  • 57

1 Answers1

2

In absence of Minimal Complete Reproducible Example one can only guess how your test files are placed in your project and why they are shown in report, because jacoco-maven-plugin doesn't show test classes in report by default - here is proof:

for the following pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <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.8.3</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

src/main/java/Example.java

class Example {
    void sayHello() {
        System.out.println("Hello, World!");
    }
}

and src/test/java/ExampleTest.java

import org.junit.Test;

public class ExampleTest {
    @Test
    public void test() {
        new Example().sayHello();
    }
}

execution of

mvn clean verify

generates following report that as you can see doesn't contain src/test/java/ExampleTest.java:

report

Trying to blindly guess, one can assume that your project has non-standard structure where the above Example.java and ExampleTest.java reside in the same src directory and you have following pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

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

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

  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src</testSourceDirectory>

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

</project>

then execution of mvn clean verify indeed will produce following report that contains ExampleTest.java

report

however in this case following exclusion of **/*Test.*

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

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

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

  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src</testSourceDirectory>

    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.3</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
              <goal>report</goal>
            </goals>
            <configuration>
              <excludes>
                <exclude>**/*Test.*</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

works just fine

report

Godin
  • 9,801
  • 2
  • 39
  • 76
  • For the record, I am seeing this same problem and do not have a non-standard setup. My tests and sources are in the standard Maven locations (`src/main` and `src/test`). I have no `sourceDirectory` or `testSourceDirectory` specified so it should also be using the defaults. Running Jacoco plugin strictly from the CLI (not via POM file) yields coverage data on the test files. – robross0606 Jun 29 '23 at 14:28