When developing software, it's crucial to know how much of your codebase is covered by tests. This repository demonstrates how to use the JaCoCo (Java Code Coverage) tool to measure code coverage in a running Java application.
For a detailed explanation, check out the full article: JaCoCo Code Coverage Guide
To download the required JaCoCo tools, run the following curl
commands:
curl -O https://repo1.maven.org/maven2/org/jacoco/org.jacoco.cli/0.8.12/org.jacoco.cli-0.8.12-nodeps.jar
curl -O https://repo1.maven.org/maven2/org/jacoco/org.jacoco.agent/0.8.12/org.jacoco.agent-0.8.12-runtime.jar
./gradlew build
java '-javaagent:org.jacoco.agent-0.8.12-runtime.jar=port=6300,address=0.0.0.0,destfile=jacoco.exec,includes=com.example.*,append=true,output=tcpserver' -jar build/libs/demo_api_server-0.0.1-SNAPSHOT.jar
Example API call:
curl http://localhost:8080/multiple?num1=11&num2=2
After running your tests or interacting with the application, dump the coverage data:
java -jar org.jacoco.cli-0.8.12-nodeps.jar dump --address localhost --port 6300 --destfile jacoco.exec --reset
Generate the coverage report to view in the browser:
java -jar org.jacoco.cli-0.8.12-nodeps.jar report jacoco.exec --classfiles build/classes/java/main --sourcefiles src/main/java --html coverage_report
- Adjust the paths and package names (
com.example.*
) according to your project structure if they differ.