JaCoCo is a tool that measures how much of your code is tested by your unit tests. It helps you see which parts of your code are covered and which are not.
Click to reveal answer
beginner
How do you add JaCoCo to a Maven project?
You add the JaCoCo plugin in your pom.xml under build > plugins. This plugin runs during the test phase and generates coverage reports.
Click to reveal answer
intermediate
What is the purpose of the jacoco-maven-plugin in the build process?
The jacoco-maven-plugin instruments the code during tests to collect coverage data and then generates reports showing which code was executed.
Click to reveal answer
beginner
Which JaCoCo report formats are commonly used?
JaCoCo can generate reports in HTML (for easy viewing in browsers), XML (for integration with other tools), and CSV formats.
Click to reveal answer
intermediate
How can you configure JaCoCo to fail the build if coverage is below a threshold?
In the JaCoCo plugin configuration, you can set rules with check goals specifying minimum coverage percentages. If coverage is below these, the build fails.
Click to reveal answer
What phase of the Maven build lifecycle does JaCoCo typically run in?
Acompile
Bpackage
Ctest
Dinstall
✗ Incorrect
JaCoCo runs during the test phase to collect coverage data while tests are executed.
Which of these is NOT a report format generated by JaCoCo?
AHTML
BXML
CCSV
DPDF
✗ Incorrect
JaCoCo does not generate PDF reports by default; it supports HTML, XML, and CSV.
How do you add JaCoCo to a Gradle project?
AApply the 'jacoco' plugin in build.gradle
BAdd JaCoCo dependency in dependencies block
CInclude JaCoCo jar in src folder
DRun JaCoCo as a separate command line tool
✗ Incorrect
In Gradle, you apply the 'jacoco' plugin in your build.gradle file to enable coverage.
What does JaCoCo instrument in your project?
ATest classes only
BSource code classes
CBuild scripts
DDocumentation files
✗ Incorrect
JaCoCo instruments the source code classes to track which parts are executed during tests.
Which Maven plugin goal generates the JaCoCo coverage report?
Ajacoco:report
Bjacoco:clean
Cjacoco:check
Djacoco:prepare-agent
✗ Incorrect
The 'jacoco:report' goal generates the coverage report after tests run.
Explain how to set up JaCoCo in a Maven project to generate coverage reports.
Think about where in the build file and lifecycle JaCoCo fits.
You got /4 concepts.
Describe how JaCoCo helps improve code quality in testing.
Consider how coverage data influences testing decisions.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of JaCoCo in a JUnit testing environment?
easy
A. To replace JUnit as a testing framework
B. To run JUnit tests faster
C. To generate test data automatically
D. To measure how much of your code is tested by your unit tests
Solution
Step 1: Understand JaCoCo's role
JaCoCo is a code coverage tool that tracks which parts of your code are executed during tests.
Step 2: Compare options
Only To measure how much of your code is tested by your unit tests correctly describes JaCoCo's purpose; others describe unrelated functions.
Final Answer:
To measure how much of your code is tested by your unit tests -> Option D
Quick Check:
JaCoCo = Code coverage tool [OK]
Hint: JaCoCo measures tested code coverage, not test speed [OK]
Common Mistakes:
Confusing JaCoCo with test runners
Thinking JaCoCo generates tests
Assuming JaCoCo replaces JUnit
2. Which of the following is the correct way to add JaCoCo plugin in a Maven pom.xml file?
easy
A.
org.jacoco
jacoco-maven-plugin
0.8.8
B.
org.junit.jupiter
junit-jupiter-engine
5.9.2
C.
org.jacoco
jacoco-core
0.8.8
D.
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
Solution
Step 1: Identify JaCoCo plugin syntax
JaCoCo is added as a Maven plugin with groupId 'org.jacoco' and artifactId 'jacoco-maven-plugin'.
Step 2: Eliminate incorrect options
Options B and D are unrelated plugins; A is a dependency, not a plugin.
Final Answer:
Correct JaCoCo plugin configuration in Maven -> Option A
Assuming coverage is collected without prepare-agent
Thinking report runs before tests
Confusing goals causing build failure
4. You added JaCoCo plugin to your Maven project but no coverage report is generated after running mvn test. What is the most likely cause?
medium
A. JUnit tests are not written correctly
B. You used an incorrect JaCoCo version number
C. You forgot to bind the report goal to the test phase
D. Maven compiler plugin is missing
Solution
Step 1: Check JaCoCo execution bindings
Without binding the 'report' goal to the 'test' phase, JaCoCo won't generate reports automatically.
Step 2: Eliminate other causes
Incorrect version usually causes build errors; test correctness or compiler plugin absence won't stop report generation if configured.
Final Answer:
Missing binding of report goal to test phase -> Option C
Quick Check:
Report goal must be bound to test phase [OK]
Hint: Bind report goal to test phase to generate coverage report [OK]
Common Mistakes:
Ignoring execution phase bindings
Blaming test code for missing reports
Assuming version mismatch causes silent failure
5. You want to configure JaCoCo in a Gradle project to generate an XML coverage report after tests run. Which snippet correctly configures this in build.gradle?