Bird
Raised Fist0
JUnittesting~20 mins

Coverage reports in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
JUnit Coverage Report Output
Given the following JUnit test and code, what will the coverage report indicate for the method calculateSum?
JUnit
public class Calculator {
    public int calculateSum(int a, int b) {
        if (a > 0 && b > 0) {
            return a + b;
        } else {
            return 0;
        }
    }
}

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class CalculatorTest {
    @Test
    public void testCalculateSumPositive() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.calculateSum(2, 3));
    }
}
A50% method coverage, 50% branch coverage
B100% method coverage, 100% branch coverage
C100% method coverage, 50% branch coverage
D50% method coverage, 100% branch coverage
Attempts:
2 left
๐Ÿ’ก Hint
Think about which parts of the method are executed by the test inputs.
โ“ assertion
intermediate
1:30remaining
Assertion for Coverage Threshold
Which JUnit assertion correctly checks that the code coverage percentage is at least 80%?
JUnit
double coveragePercentage = 75.5;
AassertTrue(coveragePercentage >= 80);
BassertEquals(coveragePercentage, 80);
CassertFalse(coveragePercentage < 80);
DassertNotNull(coveragePercentage >= 80);
Attempts:
2 left
๐Ÿ’ก Hint
Use an assertion that checks a condition is true.
๐Ÿง  Conceptual
advanced
1:30remaining
Understanding Coverage Report Metrics
Which statement best describes the difference between line coverage and branch coverage in a JUnit coverage report?
ALine coverage measures executed methods; branch coverage measures executed classes.
BLine coverage measures executed classes; branch coverage measures executed methods.
CLine coverage measures executed branches; branch coverage measures executed lines.
DLine coverage measures executed lines; branch coverage measures executed decision paths.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what a branch means in code.
๐Ÿ”ง Debug
advanced
2:00remaining
Debugging Missing Coverage
A JUnit coverage report shows 0% coverage for a method, but the test suite runs without errors. What is the most likely cause?
AThe method is never called by any test.
BThe test suite has syntax errors.
CThe coverage tool is not installed.
DThe method is private and cannot be tested.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what coverage means.
โ“ framework
expert
3:00remaining
Configuring Coverage Thresholds in JUnit
Which configuration snippet correctly sets a minimum branch coverage threshold of 85% using JaCoCo in a Maven project for JUnit tests?
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>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
    <execution>
      <id>check</id>
      <goals>
        <goal>check</goal>
      </goals>
      <configuration>
        <!-- Threshold config here -->
      </configuration>
    </execution>
  </executions>
</plugin>
A
&lt;rules&gt;
  &lt;rule&gt;
    &lt;element&gt;CLASS&lt;/element&gt;
    &lt;limits&gt;
      &lt;limit&gt;
        &lt;counter&gt;LINE&lt;/counter&gt;
        &lt;value&gt;COVEREDRATIO&lt;/value&gt;
        &lt;minimum&gt;0.85&lt;/minimum&gt;
      &lt;/limit&gt;
    &lt;/limits&gt;
  &lt;/rule&gt;
&lt;/rules&gt;
B
&lt;rules&gt;
  &lt;rule&gt;
    &lt;element&gt;BUNDLE&lt;/element&gt;
    &lt;limits&gt;
      &lt;limit&gt;
        &lt;counter&gt;BRANCH&lt;/counter&gt;
        &lt;value&gt;COVEREDRATIO&lt;/value&gt;
        &lt;minimum&gt;0.85&lt;/minimum&gt;
      &lt;/limit&gt;
    &lt;/limits&gt;
  &lt;/rule&gt;
&lt;/rules&gt;
C
&lt;rules&gt;
  &lt;rule&gt;
    &lt;element&gt;METHOD&lt;/element&gt;
    &lt;limits&gt;
      &lt;limit&gt;
        &lt;counter&gt;INSTRUCTION&lt;/counter&gt;
        &lt;value&gt;COVEREDRATIO&lt;/value&gt;
        &lt;minimum&gt;0.85&lt;/minimum&gt;
      &lt;/limit&gt;
    &lt;/limits&gt;
  &lt;/rule&gt;
&lt;/rules&gt;
D
&lt;rules&gt;
  &lt;rule&gt;
    &lt;element&gt;BUNDLE&lt;/element&gt;
    &lt;limits&gt;
      &lt;limit&gt;
        &lt;counter&gt;LINE&lt;/counter&gt;
        &lt;value&gt;COVEREDRATIO&lt;/value&gt;
        &lt;minimum&gt;0.85&lt;/minimum&gt;
      &lt;/limit&gt;
    &lt;/limits&gt;
  &lt;/rule&gt;
&lt;/rules&gt;
Attempts:
2 left
๐Ÿ’ก Hint
Branch coverage uses the BRANCH counter and is usually set at the BUNDLE level.

Practice

(1/5)
1. What is the main purpose of a coverage report in JUnit testing?
easy
A. To show which parts of the code were executed during tests
B. To list all test cases in the project
C. To measure the speed of test execution
D. To generate test data automatically

Solution

  1. Step 1: Understand coverage report purpose

    A coverage report shows which lines or branches of code were run during testing.
  2. 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.
  3. Final Answer:

    To show which parts of the code were executed during tests -> Option A
  4. Quick 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
A. JaCoCo
B. JUnitParams
C. Mockito
D. Selenium

Solution

  1. Step 1: Identify coverage tools for JUnit

    JaCoCo is a popular Java code coverage tool that integrates with JUnit.
  2. Step 2: Eliminate unrelated tools

    Selenium is for UI testing, Mockito for mocking, JUnitParams for parameterized tests, not coverage.
  3. Final Answer:

    JaCoCo -> Option A
  4. Quick 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:
Lines covered: 80
Lines missed: 20

What is the coverage percentage?
medium
A. 100%
B. 20%
C. 80%
D. 60%

Solution

  1. Step 1: Calculate total lines

    Total lines = covered + missed = 80 + 20 = 100 lines.
  2. Step 2: Calculate coverage percentage

    Coverage % = (covered / total) * 100 = (80 / 100) * 100 = 80%.
  3. Final Answer:

    80% -> Option C
  4. Quick 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
A. Tests did not execute any code
B. JaCoCo was not properly attached to the test run
C. All tests passed successfully
D. The source code is empty

Solution

  1. Step 1: Analyze 0% coverage meaning

    0% coverage means no code was recorded as executed during tests.
  2. Step 2: Identify common causes

    If tests ran but coverage is zero, JaCoCo likely was not attached or configured correctly.
  3. Final Answer:

    JaCoCo was not properly attached to the test run -> Option B
  4. Quick 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
A. Increase the number of test classes
B. Add @BranchCoverage annotation to test methods
C. Use a different testing framework instead of JUnit
D. Enable branchCoverage option in JaCoCo plugin settings

Solution

  1. Step 1: Understand branch coverage

    Branch coverage measures if all decision branches (like if/else) are tested.
  2. Step 2: Configure JaCoCo for branch coverage

    JaCoCo supports branch coverage via plugin settings; enabling branchCoverage activates it.
  3. Step 3: Eliminate incorrect options

    No annotation @BranchCoverage exists; changing framework or test count doesn't enable branch coverage reporting.
  4. Final Answer:

    Enable branchCoverage option in JaCoCo plugin settings -> Option D
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard