Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven multi module project with separate tests module - Code Coverage?

I have a maven multi module project.

root:
    moduleA/   # no unit tests
    moduleB/   # no unit tests
    moduleC/   # no unit tests
    tests/     # All unit tests, since depends on modules A, B and C

All tests are in single module called tests/ and all code is in separate modules.

Is there a way I can get code coverage?

like image 662
nishant Avatar asked Nov 13 '14 12:11

nishant


1 Answers

There is a way to accomplish this. The magic is to create a combined jacoco.exec file and to do it in two steps. My pom:

<properties>
    ...
    <jacoco.overall.exec>${maven.multiModuleProjectDirectory}/target/jacoco_analysis/jacoco.exec</jacoco.overall.exec>
</properties>

<build>
    <pluginManagement>
        <plugins>

            ...

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <configuration>
                    <destFile>${jacoco.overall.exec}</destFile>
                    <dataFile>${jacoco.overall.exec}</dataFile>
                </configuration>
            </plugin>

            ...

        </plugins>
    </pluginManagement>
</build>

<profiles>
    <profile>
        <id>runTestWithJacoco</id>
        <activation>
            <property>
                <name>runTestWithJacoco</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-prepare-agent</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <append>true</append>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>createJacocoReport</id>
        <activation>
            <property>
                <name>createJacocoReport</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-report</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Add this to your parent pom and execute mvn clean install -DrunTestWithJacoco and than mvn validate -DcreateJacocoReport. Now you have the complete coverage of a class and it doesn't matter which test covered it. The magic is to use maven.multiModuleProjectDirectory to create a combined jacoco.exec file. This property is available since maven 3.3.1 and points to the folder where you started your maven build.

like image 146
Sven Oppermann Avatar answered Oct 23 '22 12:10

Sven Oppermann