Design and Implementation of a Java Code Coverage Platform Based on JaCoCo
This article presents the design, implementation details, and workflow of a Java code coverage platform built on JaCoCo, covering full and incremental coverage collection, configuration, analysis of diff code, reporting, and future integration plans to improve testing quality and code reliability.
Introduction – Code test coverage is a metric that measures how much of the source code is exercised by tests, serving as a white‑box testing technique to expose uncovered code blocks and improve code quality.
Background – Testers aim to ensure software quality, and coverage is a key indicator of test completeness. A platform is needed to aggregate backend coverage data across iterations, including interface, functional, and automated tests, enabling analysis of test quality.
Platform Significance – The platform helps testers refine test cases, reduce dead code, and use coverage numbers as a quantitative standard for code quality, supporting both full‑project and incremental analysis.
Overall Design – The system integrates full‑code and incremental coverage statistics (based on Git commit IDs or branch names) using JaCoCo’s on‑the‑fly mode, with scheduled or manual triggers for data collection.
Full Coverage Process – Set baseline and comparison branches, collect .exec files, merge them, and generate reports via JaCoCo APIs, producing metrics such as instruction, branch, line, method, class coverage, and cyclomatic complexity.
Incremental (Diff) Coverage Process – Identify changed .java files between baseline and target commits using JGit, filter added or modified files, compute MD5 hashes of method bodies to detect changes, and generate coverage reports limited to the changed methods.
Implementation Details
Configuration of coverage dimensions (Git repo, module name, baseline/comparison versions, server IP, Java version, coverage type).
Core computation scans initialized data and performs calculations.
Clone code using JGit.
Parse pom.xml to discover effective modules.
Compile code with commands like cd to the source directory and mvn clean compile , logging output.
Analyze diff code to extract method‑level changes.
Pull .exec files from target machines via JaCoCo dump tool.
Generate coverage reports using jacoco-cli , focusing on incremental methods when applicable, and persist results to the database.
Results and Details – Supports JDK 8 and JDK 11, simplifies coverage metrics for broader understanding, enables load‑balanced collection, provides global exclusion and core‑function coverage settings, and offers visual HTML reports with package‑class‑method hierarchies.
Future Plans – Integrate with continuous testing platforms for function‑level tracing, use coverage to infer upstream/downstream impact chains, develop more custom reporting features, and resolve Lombok‑generated code coverage isolation issues.
References – https://github.com/didi/super-jacoco , https://www.jacoco.org/jacoco/trunk/doc/
Author – Ma Cai‑li, Zheng Gang, Ye Wei‑wei from the Intelligent Data Center Performance Platform Department, responsible for finance‑related development and testing.
HomeTech
HomeTech tech sharing
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.