解决maven SonarQube单测覆盖率低问题

  |   0 评论   |   0 浏览

发现问题

最近在集中搞单元测试覆盖率,是基于SonarQube扫描的,但是发现明明已经写了单元测试了,但是覆盖率就是上不去,一直停留在40%左右。

分析原因

  1. 是不是因为maven配置中跳过了单测

    如以下配置

    <build>
        <plugins>
            <!-- maven 打包时跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    

    发现并没有在代码里有这个配置

  2. 是否有sonar扫描过滤

    比如以下配置

    <properties>
        <sonar.skip>true</sonar.skip>
    </properties>
    

    发现也并没有这个配置

  3. 因为项目是多个module的,尝试把单测的类移到一个module里,发现也不行

  4. 查了资料,网上都说jacoco可以解决覆盖率问题,接着进行尝试

解决问题

通过查阅资料,决定使用jacoco插件,一开始在代码里加了配置,发现也还是不行,我的配置如下:

我的项目类似以下:

/pom.xml
/module-a/pom.xml
/module-b/pom.xml
/module-c/pom.xml

父pom文件如下:

<project>
    <groupId>xxx.example</groupId>
    <artifactId>xxx</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>module-a</module>
        <module>module-b</module>
        <module>module-c</module>
    </modules>
    <!-- ... more XML, like plugin management ... -->
</project>

模块之间存在依赖关系,如下:

<project>
    <groupId>xxx.example</groupId>
    <artifactId>module-a</artifactId> <!-- or module-b, module-c -->
    <packaging>jar</packaging>

    <!-- ... more XML, like dependencies ... -->
</project>

设置jacoco代理,在父pom里面配置

<project>
    <!-- ... other XML ... -->
    <build>
        <plugins>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
               <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>target/jacoco.exec</dataFile>
                            <outputDirectory>target/jacoco-reports</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
        </plugins>
    </build>
</project>

生成聚合 JaCoCo XML 报告

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>report-aggregate</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置一些属性

<!-- jacoco覆盖率扫描用:maven-surefire-plugin中的argLine会覆盖jacoco插件中的argLine,所以放在这里,两者的参数都会保留 -->
        <argLine>-Dfile.encoding=UTF-8</argLine>
        <sonar.coverage.exclusions>
            **/dto/**/*, **/entity/**/*, **/dao/**/*,**/producer/**Producer,
            **/bo/**/*,**/controller/**/*,**Test,*/convert/***Convert,
            **/annotation/**
        </sonar.coverage.exclusions>
        <sonar.exclusions>
            **/dto/**/*, **/entity/**/*, **/dao/**/*,**/producer/**Producer,
            **/bo/**/*,**/controller/**/*,**Test,*/convert/***Convert,
            **/annotation/**
        </sonar.exclusions>
        <!-- Point the Sonar Qube Plugin always to the same aggregated JaCoCo report -->
        <sonar.coverage.jacoco.xmlReportPaths>
            ${project.basedir}/../../target/site/jacoco-aggregate/jacoco.xml
        </sonar.coverage.jacoco.xmlReportPaths>

通过以上配置,但是发现覆盖率没有提升的很明显。

接着去看了一下jenkins里面的maven执行命令,发现原来是这里的命令有问题

image.png

发现执行的不是jacoco插件,通过改了这个配置,在重新进行执行,单测覆盖率一下子到达了60%。

排查问题思路

  1. 先检查项目中maven配置是否有问题
  2. sonarqube,jenkins的脚本配置是否正确
  3. 使用的插件是否保持统一

也可以关注我的公众号:程序之声
图片
关注公众号,领取更多资源

本文为博主原创文章,未经博主允许不得转载。