Jacoco unit test code coverage

I am converting ANT construct to Maven. I am not using Sonar.

In Maven, Jacoco doesn't seem to report the scope of the unit tests themselves, but ANT. I tried to get this for my Maven build, but I couldn't find anything.

It seems like I should add the target <include>

to the target prepare-agent

, but I'm not sure what to include. I've tried src/test/java/*

all sorts of variations of this theme as well, but none works.

How can I configure Jacoco in Maven to report unit test code coverage?

+3


source to share


1 answer


It turns out the only way to do it is to use maven-antrun-plugin

.

There is no need to add <include>

to the target prepare-agent

because all information is present in the file jacoco.exec

it generates, including the unit test code.

The target report

does not include it, and it also cannot be configured to use it. You will need to specifically set the classfiles

and properties sourcefiles

, and the Maven Jacoco plugin will not let you do this.



Hence, you need the Maven Antrun plugin, and configure and call it from there.

<plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <executions>
            <execution>
                <id>default-prepare-agent</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
                <id>default-report</id>
                <phase>package</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <taskdef name="report" classname="org.jacoco.ant.ReportTask" classpathref="maven.plugin.classpath" />
                        <report>
                            <executiondata>
                                <file file="${project.build.directory}/jacoco.exec" />
                            </executiondata>
                            <structure name="Coverage">
                                <classfiles>
                                    <fileset dir="${project.build.directory}/classes"/>
                                    <fileset dir="${project.build.directory}/test-classes"/>
                                </classfiles>
                                <sourcefiles encoding="UTF-8">
                                    <fileset dir="src/main/java"/>
                                    <fileset dir="src/test/java"/>
                                </sourcefiles>
                            </structure>
                            <check failonviolation="true" violationsproperty="violation">
                                <rule element="BUNDLE">
                                    <limit counter="INSTRUCTION" value="COVEREDRATIO" minimum="0.95" />
                                </rule>
                            </check>
                            <html destdir="${project.build.directory}/jacoco-internal"/>
                        </report>
                    </target>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.jacoco</groupId>
                <artifactId>org.jacoco.ant</artifactId>
                <version>${jacoco.version}</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

      

+2


source







All Articles