SupercovBlog

Guide

JaCoCo coverage per test: which test covers this line in Java

JaCoCo, IntelliJ IDEA and Supercov on the same Maven project, and why the one-run-per-test loop gave a wrong answer.

JaCoCo knows the line ran. Not which test ran it. A card: jacoco.exec, struck through, hits, no test names; mvn test -Dtest=... each, struck through, appended: wrong tests; supercov runs latest line names the test.

JaCoCo cannot tell you which test covers a line. Its jacoco.exec records which lines ran during the whole JVM session, not which test ran them. IntelliJ IDEA can, in the editor, when tests run from a JUnit or TestNG run configuration with its own coverage runner, but not from a Maven or Gradle build. In a build, the only JaCoCo answer is one run per test, and that loop gives wrong answers unless you delete jacoco.exec between runs.

A diagram of three tools. JaCoCo's mvn test writes jacoco.exec with hits per line, so line 11 is covered with no test named. IntelliJ IDEA's JUnit run configuration stores tests per line, in the IDE only. supercov -- mvn test stores tests per line and names the 2 tests that ran line 11.
What a JaCoCo run stores, and whether it can name the tests.

Supercov, which we make, records coverage per JUnit test in one normal mvn test or gradle test, and supercov runs latest line <file:line> lists the tests that ran a line. See below.

At a glance

ToolPer test?How
JaCoCoNoOne mvn test -Dtest=Class#method per test, deleting target/jacoco.exec before each
IntelliJ IDEAYes, in the IDESettings, Coverage: IntelliJ IDEA runner, Track per test coverage; then Show Tests Covering Line in the gutter
SupercovYesnpx supercov -- mvn test, then npx supercov runs latest line <file:line>

We measured on a small Maven project: a Discount.apply method, three tests for it and one checkout test that calls it, with JaCoCo 0.8.16, Maven 3.9.16 and JUnit 5.13.4 on JDK 26. Line 9, return cents - 1000;, runs in one test. Line 11, return cents;, runs in two.

A grid of four methods against four tests, half, tenoff at least 10, tenoff under 10 and checkout, for line 9. The JaCoCo report names none. One run per test with jacoco.exec appended names three, wrongly. One run per test with jacoco.exec deleted names tenoff at least 10, in 4 runs. supercov runs latest line names the same test in 1 run.
Which tests each method named for line 9 of Discount.java.

JaCoCo: one run per test, and delete jacoco.exec

The JaCoCo report marks line 11 green, fully covered, with no tests named. Its Sessions page lists JVM sessions, not tests. JaCoCo’s maintainer put it plainly: “JaCoCo does not support this out of the box”. His recipe is to write a JUnit listener that dumps and resets the execution data after every test, analyse each dump, and combine them.

To find the tests by hand, run each test alone and read the report after it:

rm -f target/jacoco.exec
mvn test -Dtest='DiscountTest#tenoffDoesNothingUnderTen'

Leave out the rm and the answer is wrong. The agent’s append option is true by default, so each run adds to the last one’s data. In our loop the third test, tenoffDoesNothingUnderTen, was reported as running line 9, which it never reaches: line 9 had run in the test before it. With the file deleted each time, every test got its own correct answer.

The cost grows with the suite: four tests took 4.8 s this way against 1.3 s for one run of all four. A suite of 2,000 tests means 2,000 Maven runs.

IntelliJ IDEA: in the editor only

IntelliJ’s own coverage runner can track per-test coverage. Its documentation lists the conditions: the IntelliJ IDEA runner, not JaCoCo; Track per test coverage selected; tests run through a JUnit or TestNG run configuration. Then click a covered line’s gutter and choose Show Tests Covering Line.

That answers it for one developer in the editor. It does not run in CI, and a build’s mvn test or gradle test does not produce it.

Supercov: one Maven run

npx supercov -- mvn test
npx supercov runs latest line src/main/java/shop/Discount.java:11
src/main/java/shop/Discount.java:11

Source
     11 | return cents;

Status
  COVERED
  Evidence: execution only

Covering tests
  shop.CheckoutTest#checkoutAddsUpTheItems() [.::shop.CheckoutTest#checkoutAddsUpTheItems()] — unit/junit-platform
  shop.DiscountTest#tenoffDoesNothingUnderTen() [.::shop.DiscountTest#tenoffDoesNothingUnderTen()] — unit/junit-platform

Line 9 named only tenoffTakesTenOffOrdersOfTenOrMore. The run took 1.8 s, the same Maven build with Supercov’s agent added. Use gradle test or ./gradlew test after -- for Gradle; JUnit 5, JUnit 4 through Vintage, and Kotlin tests on JDK 17 or newer are supported. Add --json, or several lines at once, for a script or a coding agent.

To keep JaCoCo’s reports for SonarQube or a coverage gate, keep it: Supercov does not replace the plugin in your build. See JaCoCo vs SonarQube and Java code coverage tools. For the same question in Vitest, Jest and pytest, see which test covers this line.

More coverage guides