All jobs

Add JaCoCo Coverage to a Gradle Spring Boot Project

Yes. An agent adds JaCoCo to your Gradle build with XML and HTML reports, excludes generated code, sets a coverage floor from the measured number, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts from build.gradle, because where coverage goes depends on what the build already generates. In a project like gothinkster/spring-boot-realworld-example-app, the plugins block applies Spring Boot 2.6.3, the dependency management plugin, java, Netflix DGS codegen 5.0.6 and Spotless 6.2.1 with googleJavaFormat, source and target compatibility are Java 11, and gradle/wrapper/gradle-wrapper.properties pins Gradle 7.4. The test task uses JUnit Platform, clean deletes the SQLite dev.db, and generateJava writes the GraphQL types into build/generated under the io.spring.graphql package. There are 23 test classes under src/test/java, API tests on REST Assured's MockMvc support and repository tests on MyBatis, and .github/workflows/gradle.yml runs ./gradlew clean test on JDK 11 with a cache of the Gradle directories. No coverage tool is configured.

It applies the jacoco plugin and sets toolVersion explicitly rather than taking Gradle 7.4's default, so the JaCoCo release does not change silently with a wrapper upgrade. jacocoTestReport depends on test, turns on the XML report that a pull request annotation or an external service reads and the HTML report a person opens, and test is finalizedBy it so one ./gradlew test always leaves a fresh report in build/reports/jacoco/test. The class directories are filtered to drop io/spring/graphql/types/** and DgsConstants, which DGS generates from schema.graphqls and nobody tests directly, so the numbers describe code the team wrote. Gradle 7's required and outputLocation properties are used, not the enabled and destination ones Gradle 7.4 deprecates.

jacocoTestCoverageVerification gets one rule on line coverage for the whole bundle, with a minimum set a little under the figure the first run measured, so the check fails on a drop and not on the day it lands, and check depends on it. The pull request states the measured percentage and the floor it chose, and says how to raise the floor as coverage improves. Spotless is left as configured; the agent runs spotlessJavaCheck because the plugin wires it into check, and the new Groovy in build.gradle is formatted to match.

In .github/workflows/gradle.yml the test step becomes ./gradlew clean test jacocoTestReport jacocoTestCoverageVerification, and an upload-artifact step keeps the HTML report and the XML file for every run so a reviewer can open the report from the Actions page. The agent installs JDK 11 in its sandbox, runs the full build with the new tasks, opens build/reports/jacoco/test/html/index.html to read the totals per package, runs it a second time to confirm the verification passes against the floor, and opens the pull request with those totals in the description.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add JaCoCo coverage to this Gradle Spring Boot project. Read build.gradle, gradle/wrapper/gradle-wrapper.properties, README.md, .gitignore, .github/workflows/gradle.yml and the test classes under src/test/java first. Apply the jacoco plugin with an explicit toolVersion. Configure jacocoTestReport to depend on test, produce XML and HTML reports using the Gradle 7 required and outputLocation properties, and exclude the DGS generated classes under io/spring/graphql/types and DgsConstants from the class directories; make test finalizedBy jacocoTestReport. Add jacocoTestCoverageVerification with one line-coverage rule whose minimum sits just under the measured total, and make check depend on it. Update the GitHub Actions workflow to run the report and verification after the tests and upload the HTML and XML reports as an artifact. Run spotlessJavaCheck. Java 11 is not installed here: install it, run ./gradlew clean test jacocoTestReport jacocoTestCoverageVerification twice, and open a pull request that states the measured coverage and the floor.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that changes build.gradle and .github/workflows/gradle.yml and nothing else. The description gives the measured line and branch coverage for the whole project and for the api, application, core and infrastructure packages, the floor it set, the JaCoCo version it pinned, and the path of the HTML report on a CI run.

The run's record holds the JDK install, both Gradle runs with the 23 test classes passing, the report's totals read from the generated HTML, and the Spotless check, so the percentage in the description is a number the run printed and not an estimate.

Before merging, a reviewer decides whether the floor should be per package rather than whole-project, whether branch coverage should have a rule too, and whether to post the XML report to a coverage service or a pull request comment. The tests, sources and the generated GraphQL code are unchanged.

Run it on every Java service with no coverage number

A team with several Gradle services starts one run per repository from its own server: one POST /o/{org}/sessions each, with that repository in resources and a request_id that is a UUID v5 of the repository's full name, so a retried call returns the run already started instead of a second pull request. Each run measures its own project before it chooses a floor, so no two services get the same arbitrary number.

After that, an automation on the Project with a github trigger on pull_request.opened and pull_request.synchronize and the prompt "run the tests with the coverage report and comment with the total line coverage and the change against the base branch" turns every pull request into a before-and-after. The trigger puts the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

The usual coverage pull request picks a round threshold from habit, counts generated code it cannot test, and fails the first CI run on the day it merges. The agent runs the suite on its own machine first, reads the number the report gives, excludes the code the build generated, and sets the floor from what it measured, so the check that lands is one the project passes today and fails tomorrow only on a real drop.

Every command stays with the run, from the JDK install to the second Gradle run that proves the verification passes, and the pull request cites them. A team can start the same job on each of its Gradle services at once, and each pull request arrives with its own measured number.

Frequently asked questions

Why exclude the generated GraphQL classes?

The DGS codegen plugin writes the types for schema.graphqls into build/generated on every build, and they are data classes nobody writes tests for. Counting them drags the total down with lines the team does not own and makes a schema change move the coverage number. Filtering classDirectories keeps them out of the report and the verification while they stay compiled and on the classpath.

Why set the floor from the measured number rather than a target?

A floor above today's coverage fails every build until someone writes tests, and in practice it is lowered or ignored. A floor just under the measured figure passes today and catches a pull request that removes tests or adds untested code. The pull request names both numbers, and raising the floor later is a one-line change.

Why pin toolVersion?

Each Gradle release carries its own default JaCoCo version, so a wrapper upgrade can change the agent that instruments the tests and, with it, the numbers. Setting toolVersion makes the coverage tool a deliberate dependency the way build.gradle already pins MyBatis, DGS and REST Assured.

Does this slow the test run?

JaCoCo attaches an agent to the test JVM and writes build/jacoco/test.exec, which adds a small fraction to a suite this size; the report task itself takes seconds. The agent reports both run times from its two builds in the pull request so the reviewer can see the cost rather than guess it.

More Gradle jobs

The same job on other stacks

More jobs like this: Hand off a long task

Keep reading