具有集成测试的 Jacoco 多模块 - 未涵盖 REST 控制器

Jacoco multimodule with integration tests - REST Controllers not covered

我在应用程序(网络、业务...)上有多个层。 我想使用 jacoco-maven-plugin 在声纳上发布每个测试(单元测试和集成测试)的覆盖范围。

在应用程序中,所有代码都被单元测试和集成测试覆盖,pom.xml 是正确的,但我遇到了这个问题:

控制器代码不被认为包含在 jacoco 报告中的测试中,这与这些控制器调用的服务(业务层)不同!

示例:

FunctionalityAService.javaFunctionalityAServiceTest.java 覆盖(单元测试)

FunctionalityBService.java 涵盖,FunctionalityBController.java 未涵盖 FunctionalityBControllerIT.java(集成测试)

你可以在这里找到一个例子:https://github.com/salimchami/maven-jacoco-multimodule-sonar

如何覆盖控制器?

编辑 1:

我不明白为什么必须创建一个模块来聚合结果...为什么我们不能在根模块(根模块中的 jacoco-aggregate 目录)中执行此操作?

编辑 2:

这是一个 spring 引导项目,我在集成测试中使用 spring mockMvc。 因此,控制器不会被实例化调用。

我找到了解决方案:

根 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>

覆盖模块 pom :

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

<dependencies>
<!-- list of all sub modules dependencies -->
</dependencies>

子模块示例:

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