Coverage reports show which parts of your code were tested. They help find untested code so you can improve your tests.
Coverage reports in JUnit
Introduction
Syntax
JUnit
Use a coverage tool like JaCoCo with JUnit tests. Example Maven plugin configuration: <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.8</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
Coverage tools run alongside your tests to collect data.
Reports can be in HTML, XML, or other formats for easy reading.
Examples
JUnit
mvn clean test mvn jacoco:report
JUnit
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>Sample Program
This test covers only the add method of Calculator. The subtract method is not tested, so coverage report will show partial coverage.
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } public class CalculatorTest { @Test public void testAdd() { Calculator calc = new Calculator(); assertEquals(5, calc.add(2, 3)); } }
Important Notes
Coverage does not guarantee bug-free code but helps find untested parts.
High coverage is good, but tests must also check correct behavior.
Use coverage reports regularly to improve test quality over time.
Summary
Coverage reports show which code is tested and which is not.
Use tools like JaCoCo with JUnit to generate coverage reports.
Review reports to improve your tests and software quality.
Practice
1. What is the main purpose of a
coverage report in JUnit testing?easy
Solution
Step 1: Understand coverage report purpose
A coverage report shows which lines or branches of code were run during testing.Step 2: Compare options to definition
Only To show which parts of the code were executed during tests matches this purpose; others describe unrelated features.Final Answer:
To show which parts of the code were executed during tests -> Option AQuick Check:
Coverage report = executed code lines [OK]
Hint: Coverage reports highlight tested code lines quickly [OK]
Common Mistakes:
- Confusing coverage with test case listing
- Thinking coverage measures test speed
- Assuming coverage generates test data
2. Which tool is commonly used with JUnit to generate coverage reports?
easy
Solution
Step 1: Identify coverage tools for JUnit
JaCoCo is a popular Java code coverage tool that integrates with JUnit.Step 2: Eliminate unrelated tools
Selenium is for UI testing, Mockito for mocking, JUnitParams for parameterized tests, not coverage.Final Answer:
JaCoCo -> Option AQuick Check:
Coverage tool with JUnit = JaCoCo [OK]
Hint: JaCoCo is the go-to coverage tool for Java tests [OK]
Common Mistakes:
- Confusing Selenium with coverage tools
- Mixing mocking frameworks with coverage
- Assuming JUnitParams generates coverage
3. Given this JaCoCo coverage summary:
What is the coverage percentage?
Lines covered: 80
Lines missed: 20What is the coverage percentage?
medium
Solution
Step 1: Calculate total lines
Total lines = covered + missed = 80 + 20 = 100 lines.Step 2: Calculate coverage percentage
Coverage % = (covered / total) * 100 = (80 / 100) * 100 = 80%.Final Answer:
80% -> Option CQuick Check:
80 covered of 100 total = 80% [OK]
Hint: Coverage % = covered lines รท total lines x 100 [OK]
Common Mistakes:
- Using missed lines as coverage
- Forgetting to add covered and missed for total
- Mixing up numerator and denominator
4. You ran tests with JaCoCo but the coverage report shows 0% coverage. What is the most likely cause?
medium
Solution
Step 1: Analyze 0% coverage meaning
0% coverage means no code was recorded as executed during tests.Step 2: Identify common causes
If tests ran but coverage is zero, JaCoCo likely was not attached or configured correctly.Final Answer:
JaCoCo was not properly attached to the test run -> Option BQuick Check:
0% coverage usually means missing JaCoCo setup [OK]
Hint: Check JaCoCo setup if coverage is zero [OK]
Common Mistakes:
- Assuming tests passed means coverage is full
- Thinking empty source code is common
- Ignoring JaCoCo configuration errors
5. You want to improve your test coverage report by including branch coverage in JaCoCo. Which configuration change is needed?
hard
Solution
Step 1: Understand branch coverage
Branch coverage measures if all decision branches (like if/else) are tested.Step 2: Configure JaCoCo for branch coverage
JaCoCo supports branch coverage via plugin settings; enablingbranchCoverageactivates it.Step 3: Eliminate incorrect options
No annotation@BranchCoverageexists; changing framework or test count doesn't enable branch coverage reporting.Final Answer:
EnablebranchCoverageoption in JaCoCo plugin settings -> Option DQuick Check:
Branch coverage = enable in JaCoCo config [OK]
Hint: Enable branchCoverage in JaCoCo config to track branches [OK]
Common Mistakes:
- Looking for annotations to enable coverage
- Thinking more tests auto-enable branch coverage
- Switching frameworks unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
