0
0
JUnittesting~20 mins

Coverage reports in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.