Search code examples
spring-bootmavencode-coveragejacoco-maven-plugin

Jacoco multimodule with integration tests - REST Controllers not covered


I have mutliple layers on an application (web, business...). I want to publish coverage of each test (unit and integration tests) on sonar by using jacoco-maven-plugin.

In the application, all the code is covered by unit and integration tests, and the pom.xml is correct but I have this problem:

The controllers code is not considered to be covered by the tests in jacoco reports, unlike the services (business layer) called by these controllers !

Example :

FunctionalityAService.java covered by FunctionalityAServiceTest.java (unit test)

FunctionalityBService.java covered, FunctionalityBController.java not covered by FunctionalityBControllerIT.java (integration test)

You can find an example here : https://github.com/salimchami/maven-jacoco-multimodule-sonar

How to include coverage on Controllers ?

EDIT 1 :

I don't understand why it's mandatory to create a module only to aggragate results... Why We can't do this in the root module (jacoco-aggregate directory in the root module) ?

EDIT 2 :

It's a spring boot project and I'm using spring mockMvc in the integration tests. So, the controllers are not called by an instantiation.


Solution

  • I found a solution :

    Root pom :

    <modules>
        <module>dao</module>
        <module>domain</module>
        <module>web</module>
        <module>coverage</module>
    </modules>
     
    <properties>
        <project.xmlReportPaths>coverage/target/site/jacoco-aggregate/jacoco.xml</project.xmlReportPaths>
    </properties>
     
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                <plugin>
                    <groupId>org.sonarsource.scanner.maven</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                    <version>3.7.0.1746</version>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.5</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M4</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>3.0.0-M4</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/*IT.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report-aggregate</id>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                        <phase>verify</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
     
    <profiles>
        <profile>
            <id>its</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/*IT.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <id>integration-test</id>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>it-agent</id>
                                <goals>
                                    <goal>prepare-agent-integration</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>it-report</id>
                                <goals>
                                    <goal>report-integration</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    coverage module pom :

    <properties>
        <sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/../${project.xmlReportPaths}
            </sonar.coverage.jacoco.xmlReportPaths>
    </properties>
    
    <dependencies>
    <!-- list of all sub modules dependencies -->
    </dependencies>
    

    example of sub module :

    <properties>
        <sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/../${project.xmlReportPaths}</sonar.coverage.jacoco.xmlReportPaths>
    </properties>