Springboot 多模块项目集成Jacoco统计单元测试覆盖率

最外层POM配置

<plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.18.1version>
                <configuration>
                    <testFailureIgnore>truetestFailureIgnore>
                    <forkMode>onceforkMode>
                    <reuseForks>truereuseForks>
                    <useSystemClassLoader>falseuseSystemClassLoader>
                    <argLine>@{argLine}argLine>
                    <includes>
                        <include>**/*Test.javainclude>
                        <include>**/*Test*.javainclude>
                    includes>
                configuration>
            plugin>
            <plugin>
                <groupId>org.jacocogroupId>
                <artifactId>jacoco-maven-pluginartifactId>
                <version>0.8.6version>
                <executions>
                    <execution>
                        <id>my-prepare-agentid>
                        <goals>
                            <goal>prepare-agentgoal>
                        goals>
                        <configuration>
                            <propertyName>surefireArgLinepropertyName>
                        configuration>
                    execution>
                    <execution>
                        <id>my-reportid>
                        <phase>testphase>
                        <goals>
                            <goal>report-aggregategoal>
                        goals>
                    execution>
                executions>
            plugin>

本地统计代码单元测试覆盖率

mvn test jacoco:prepare-agent jacoco:report jacoco:report-aggregate 

远端SonarQube执行

jenkis服务器需要配置Jacoco

  mvn test jacoco:prepare-agent jacoco:report jacoco:report-aggregate sonar:sonar -Dsonar.projectKey=xxxxx -Dsonar.projectName=xxx

注意事项

Q1 target目录下未编译test目录下的代码

常见报错:

Not compiling test sources

Skipping JaCoCo execution due to missing execution data file.

修改插件配置

 <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>${version.compiler.plugin}version>
                <configuration>
                     
                    <skip>falseskip>
                    
                configuration>
 plugin>

你可能感兴趣的:(单元测试,代码覆盖率)