jacoco

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

JaCoCo - Quick Reference

JaCoCo - 快速参考

When to Use This Skill

何时使用该技能

  • Configure code coverage in Java/Maven projects
  • Set coverage thresholds
  • Integrate coverage in CI/CD
  • 在Java/Maven项目中配置代码覆盖率
  • 设置覆盖率阈值
  • 在CI/CD中集成覆盖率统计

When NOT to Use This Skill

何时不应使用该技能

  • JavaScript/TypeScript coverage - Use Vitest skill for frontend coverage
  • SonarQube configuration - Use
    sonarqube
    skill for quality gates
  • Test writing - Use Spring Boot Test or JUnit skills
  • Gradle projects - JaCoCo works but syntax differs from Maven
Deep Knowledge: Use
mcp__documentation__fetch_docs
with technology:
jacoco
for comprehensive documentation.
  • JavaScript/TypeScript覆盖率 - 前端覆盖率统计请使用Vitest skill
  • SonarQube配置 - 质量门相关配置请使用
    sonarqube
    skill
  • 测试用例编写 - 请使用Spring Boot Test或JUnit相关skill
  • Gradle项目 - JaCoCo支持Gradle但语法与Maven不同
深度知识:使用
mcp__documentation__fetch_docs
指定技术为
jacoco
,可获取完整文档。

Essential Patterns

核心配置示例

Maven Configuration

Maven配置

xml
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.12</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals><goal>prepare-agent</goal></goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals><goal>report</goal></goals>
        </execution>
        <execution>
            <id>check</id>
            <goals><goal>check</goal></goals>
            <configuration>
                <rules>
                    <rule>
                        <element>BUNDLE</element>
                        <limits>
                            <limit>
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>
xml
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.12</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals><goal>prepare-agent</goal></goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals><goal>report</goal></goals>
        </execution>
        <execution>
            <id>check</id>
            <goals><goal>check</goal></goals>
            <configuration>
                <rules>
                    <rule>
                        <element>BUNDLE</element>
                        <limits>
                            <limit>
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

Exclusions

排除规则

xml
<excludes>
    <exclude>com.company.config.*</exclude>
    <exclude>com.company.dto.*</exclude>
    <exclude>com.company.entity.*</exclude>
    <exclude>com.company.mapper.*Impl</exclude>
</excludes>
xml
<excludes>
    <exclude>com.company.config.*</exclude>
    <exclude>com.company.dto.*</exclude>
    <exclude>com.company.entity.*</exclude>
    <exclude>com.company.mapper.*Impl</exclude>
</excludes>

Commands

常用命令

bash
mvn clean verify           # Test + coverage check
mvn jacoco:report          # Generate report
open target/site/jacoco/index.html  # View report
bash
mvn clean verify           # 执行测试 + 覆盖率检查
mvn jacoco:report          # 生成覆盖率报告
open target/site/jacoco/index.html  # 查看报告

Counter Types

统计维度

CounterDescription
LINE
Lines of code
BRANCH
Branch if/switch
METHOD
Methods
统计维度说明
LINE
代码行
BRANCH
if/switch分支
METHOD
方法

Anti-Patterns

错误用法

Anti-PatternWhy It's BadCorrect Approach
Aiming for 100% coverageDiminishing returns, test bloatTarget 80% LINE, 70% BRANCH
Including DTOs/entities in coverageInflates numbers, no logic to testExclude with
<excludes>
Only checking LINE coverageMisses untested branchesCheck both LINE and BRANCH
No coverage threshold in CICan't enforce qualityAdd
<check>
goal with minimums
Excluding too much codeFalse sense of securityOnly exclude generated/config code
Not versioning jacoco.execCan't track coverage trendsArchive reports in CI
错误用法不良影响正确做法
追求100%覆盖率边际效益递减,测试用例冗余目标设为80%行覆盖率、70%分支覆盖率
将DTO/实体类纳入覆盖率统计虚高覆盖率数值,这些类无业务逻辑需测试通过
<excludes>
标签排除
仅检查行覆盖率遗漏未测试的分支同时检查行覆盖率和分支覆盖率
CI中未设置覆盖率阈值无法强制保障代码质量添加
<check>
目标并设置最低覆盖率要求
排除过多代码产生代码质量合格的错觉仅排除自动生成的代码/配置类代码
不对jacoco.exec进行版本管理无法追踪覆盖率变化趋势在CI中归档覆盖率报告

Quick Troubleshooting

常见问题排查

IssueLikely CauseSolution
Coverage report shows 0%Tests not running with agentEnsure
prepare-agent
goal runs before tests
Build fails on coverage checkCoverage below thresholdAdd tests or adjust minimum in
<limit>
Report excludes nothingWrong exclude patternUse fully qualified names (com.company.dto.*)
Report missing classesClasses not loaded during testsAdd integration tests to cover them
Coverage lower in CI than localDifferent test executionEnsure CI runs all test phases
SonarQube shows no coverageWrong report pathCheck
sonar.coverage.jacoco.xmlReportPaths
问题可能原因解决方案
覆盖率报告显示0%测试运行时未加载agent确保
prepare-agent
目标在测试执行前运行
覆盖率检查导致构建失败覆盖率低于阈值补充测试用例或调整
<limit>
中的最低要求
报告未排除任何内容排除规则配置错误使用全限定类名格式(如com.company.dto.*)
报告缺失部分类测试过程中未加载对应类补充集成测试覆盖这些类
CI环境下覆盖率低于本地测试执行逻辑不同确保CI运行所有测试阶段
SonarQube未显示覆盖率数据报告路径配置错误检查
sonar.coverage.jacoco.xmlReportPaths
配置

Further Reading

扩展阅读

For advanced configurations:
mcp__documentation__fetch_docs
  • Technology:
    jacoco
    (not yet in MCP - use official docs)
  • JaCoCo Docs
进阶配置请查阅:
mcp__documentation__fetch_docs