Challenge - 5 Problems
Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)); } }
Attempts:
2 left
💡 Hint
Think about which parts of the method are executed by the test inputs.
✗ Incorrect
The test calls calculateSum with positive numbers, so the 'if' branch is taken, but the 'else' branch is not. The method is fully executed, so method coverage is 100%, but branch coverage is only 50% because one branch is not tested.
❓ assertion
intermediate1:30remaining
Assertion for Coverage Threshold
Which JUnit assertion correctly checks that the code coverage percentage is at least 80%?
JUnit
double coveragePercentage = 75.5;Attempts:
2 left
💡 Hint
Use an assertion that checks a condition is true.
✗ Incorrect
assertTrue checks if the condition is true. Here, it verifies coveragePercentage is at least 80. assertEquals checks equality, which is not what we want. assertFalse would invert the condition, and assertNotNull is for objects, not booleans.
🧠 Conceptual
advanced1:30remaining
Understanding Coverage Report Metrics
Which statement best describes the difference between line coverage and branch coverage in a JUnit coverage report?
Attempts:
2 left
💡 Hint
Think about what a branch means in code.
✗ Incorrect
Line coverage counts how many lines of code ran. Branch coverage counts how many possible paths (like if-else decisions) were taken during tests.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about what coverage means.
✗ Incorrect
Coverage measures which code runs during tests. If a method is never called, coverage is zero even if tests pass. Syntax errors or missing tools would cause test failures or no report. Private methods can be covered if called indirectly.
❓ framework
expert3: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>Attempts:
2 left
💡 Hint
Branch coverage uses the BRANCH counter and is usually set at the BUNDLE level.
✗ Incorrect
To enforce branch coverage threshold, the counter must be BRANCH and element BUNDLE. Other options set thresholds for line or instruction coverage or at different element levels.