jacoco
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJaCoCo - 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 skill for quality gates
sonarqube - Test writing - Use Spring Boot Test or JUnit skills
- Gradle projects - JaCoCo works but syntax differs from Maven
Deep Knowledge: Usewith technology:mcp__documentation__fetch_docsfor comprehensive documentation.jacoco
- JavaScript/TypeScript覆盖率 - 前端覆盖率统计请使用Vitest skill
- SonarQube配置 - 质量门相关配置请使用skill
sonarqube - 测试用例编写 - 请使用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 reportbash
mvn clean verify # 执行测试 + 覆盖率检查
mvn jacoco:report # 生成覆盖率报告
open target/site/jacoco/index.html # 查看报告Counter Types
统计维度
| Counter | Description |
|---|---|
| Lines of code |
| Branch if/switch |
| Methods |
| 统计维度 | 说明 |
|---|---|
| 代码行 |
| if/switch分支 |
| 方法 |
Anti-Patterns
错误用法
| Anti-Pattern | Why It's Bad | Correct Approach |
|---|---|---|
| Aiming for 100% coverage | Diminishing returns, test bloat | Target 80% LINE, 70% BRANCH |
| Including DTOs/entities in coverage | Inflates numbers, no logic to test | Exclude with |
| Only checking LINE coverage | Misses untested branches | Check both LINE and BRANCH |
| No coverage threshold in CI | Can't enforce quality | Add |
| Excluding too much code | False sense of security | Only exclude generated/config code |
| Not versioning jacoco.exec | Can't track coverage trends | Archive reports in CI |
| 错误用法 | 不良影响 | 正确做法 |
|---|---|---|
| 追求100%覆盖率 | 边际效益递减,测试用例冗余 | 目标设为80%行覆盖率、70%分支覆盖率 |
| 将DTO/实体类纳入覆盖率统计 | 虚高覆盖率数值,这些类无业务逻辑需测试 | 通过 |
| 仅检查行覆盖率 | 遗漏未测试的分支 | 同时检查行覆盖率和分支覆盖率 |
| CI中未设置覆盖率阈值 | 无法强制保障代码质量 | 添加 |
| 排除过多代码 | 产生代码质量合格的错觉 | 仅排除自动生成的代码/配置类代码 |
| 不对jacoco.exec进行版本管理 | 无法追踪覆盖率变化趋势 | 在CI中归档覆盖率报告 |
Quick Troubleshooting
常见问题排查
| Issue | Likely Cause | Solution |
|---|---|---|
| Coverage report shows 0% | Tests not running with agent | Ensure |
| Build fails on coverage check | Coverage below threshold | Add tests or adjust minimum in |
| Report excludes nothing | Wrong exclude pattern | Use fully qualified names (com.company.dto.*) |
| Report missing classes | Classes not loaded during tests | Add integration tests to cover them |
| Coverage lower in CI than local | Different test execution | Ensure CI runs all test phases |
| SonarQube shows no coverage | Wrong report path | Check |
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 覆盖率报告显示0% | 测试运行时未加载agent | 确保 |
| 覆盖率检查导致构建失败 | 覆盖率低于阈值 | 补充测试用例或调整 |
| 报告未排除任何内容 | 排除规则配置错误 | 使用全限定类名格式(如com.company.dto.*) |
| 报告缺失部分类 | 测试过程中未加载对应类 | 补充集成测试覆盖这些类 |
| CI环境下覆盖率低于本地 | 测试执行逻辑不同 | 确保CI运行所有测试阶段 |
| SonarQube未显示覆盖率数据 | 报告路径配置错误 | 检查 |
Further Reading
扩展阅读
For advanced configurations:mcp__documentation__fetch_docs
- Technology:
(not yet in MCP - use official docs)jacoco- JaCoCo Docs
进阶配置请查阅:mcp__documentation__fetch_docs
- 技术:
(暂未接入MCP,请参考官方文档)jacoco- JaCoCo官方文档