☕Grading test coverage with JaCoCo
Writing good unit tests is a skill students need to develop, which means you need a way to grade the tests they write — not just whether they pass, but how thoroughly they exercise the code. JaCoCo is a Java code coverage library that measures what percentage of a reference implementation is exercised by a set of unit tests. The higher the coverage, the more of the reference code the student's tests are touching.
This guide walks through uploading a reference implementation alongside the JaCoCo jars, running the student's tests under the JaCoCo agent, and converting the resulting coverage data into a numeric grade.
Before you start
You will need the following files ready to upload:
jacocoagent.jar— the JaCoCo Java agent. Attached to the JVM with-javaagent, it instruments classes at runtime and records which lines are executed.jacococli.jar— the JaCoCo command-line tool. It converts the raw coverage data into an XML report that can be graded.cov_grade.sh— a shell script that reads the XML report and prints a numeric grade for the rubric. See the Appendix for the full script.Your reference implementation (e.g.
Calculator.java).
Both JaCoCo jars can be downloaded from the official JaCoCo release page.
Step 1: Install the JUnit console launcher
JaCoCo instruments the JVM directly, so the standard cg junit5 helpers cannot be used here. Instead, download the JUnit5 platform console launcher jar from Maven Central during Setup.
Navigate to the Setup tab under the AutoTest settings and install JUnit5.

Navigate to the Test tab and add an Allow Internet block.
Add a Script block inside the Allow Internet block and paste the following:
Step 2: Compile the student's tests against a reference implementation
In Tests tab, add an Upload Files block and attach
jacocoagent.jar,jacococli.jar,cov_grade.sh, and your reference implementation (e.g.Calculator.java).Add a Connect Rubric block and select the appropriate rubric category from the drop-down menu.
Add a Script block nested inside the Connect Rubric block and paste the following:
Step 3: Run the tests under JaCoCo and grade the coverage
This step uses two Custom Test blocks nested inside the same Connect Rubric block from Step 2.
Custom Test 1 — Run student unit tests
Add a Custom Test block and paste the following:
Custom Test 2 — Grade code coverage
Add a second Custom Test block immediately after and paste the following:
The directory structure (src, tests, compiled) is required by JaCoCo's report command. Skipping or renaming these folders will cause the report step to fail.
Conclusion
You have set up an automatically graded assignment that measures how thoroughly students test a reference implementation. For background on standard instructor-written JUnit 5 tests, or for adding Checkstyle to your Java assignments, see the guides below. For questions about custom grading scripts or coverage thresholds, reach out to our support team at support@codegrade.com.
Last updated