Design and Implementation of a JaCoCo-Based Code Coverage Tool for Service Testing and CI Integration
This article describes how Dada built a JaCoCo-powered code coverage platform for Java services deployed on Kubernetes, covering tool selection, on‑the‑fly instrumentation, data collection, incremental coverage analysis, CI gate enforcement, encountered pitfalls, and future enhancements.
Background Dada aims to improve test case completeness and detect dead code by measuring code coverage, using coverage data to guide test design and reduce missed defects in production.
Tool Selection and Implementation JaCoCo was chosen for its open‑source nature, active community, and comprehensive counters (instructions, branches, cyclomatic complexity, lines, methods, classes). Only line coverage is currently used.
Instrumentation and Data Collection The on‑the‑fly mode is employed by adding the JaCoCo Java agent to the JVM start‑up command in the Docker‑compose/YAML file. The required steps are:
1) Place jacocoagent.jar in a designated path;
2) Add -javaagent:/path/to/jacocoagent.jar to the container's command line.
A central controller service (spiderKing) schedules data collector agents (spiderKnight) that periodically retrieve exec files, fetch source code via Git, obtain class files via FTP, and generate HTML, CSV, and XML reports stored in MySQL and static storage.
Export and Reporting Collected coverage data is merged when services restart or rebuild, using JaCoCo’s mergeTask to inherit exec files for the same branch, ensuring continuity across deployments.
Incremental Coverage To focus on newly changed code, Dada extracts Git diffs via the /api/v4//projects/{0}/repository/compare endpoint, parses the full‑coverage XML, and calculates coverage only for added or modified lines, treating yellow (partially covered) as covered.
CI Integration and Coverage Gates The coverage tool is integrated into the CI pipeline to measure interface automation, developer self‑testing, and pre‑release quality. A merge‑request gate blocks merges when incremental coverage falls below an 85% threshold, with notifications sent to developers and testers.
Pitfalls and Lessons Learned Issues encountered include excessive exec file size from repeated inheritance, class‑id mismatches due to differing JDK versions, inability to inherit coverage after any code change, and incomplete reports when multiple sub‑modules are present; solutions involve filtering, JDK unification, and proper classdumpdir configuration.
Future Plans Upcoming work includes handling exceptional code paths with mock platforms, integrating traffic replay for uncovering uncovered dead code, and extending coverage measurement to client‑side applications.
Dada Group Technology
Sharing insights and experiences from Dada Group's R&D department on product refinement and technology advancement, connecting with fellow geeks to exchange ideas and grow together.
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.