Challenge - 5 Problems
Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Line coverage in JUnit test
Given the following Java method and its JUnit test, what is the line coverage percentage after running the test?
JUnit
public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAdd() { Calculator calc = new Calculator(); assertEquals(5, calc.add(2, 3)); } }
Attempts:
2 left
💡 Hint
Only the add method is tested, subtract is not called.
✗ Incorrect
The Calculator class has two methods: add and subtract. The test only calls add, so only the lines inside add are executed. Since subtract is not called, its lines are not covered. There are 4 lines of code in total (2 lines per method), and only 2 lines are executed, so coverage is 50%.
❓ assertion
intermediate2:00remaining
Branch coverage assertion in JUnit
Consider this method with a conditional branch and its JUnit test. Which assertion correctly verifies full branch coverage?
JUnit
public class NumberChecker { public boolean isPositive(int number) { if (number > 0) { return true; } else { return false; } } } import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class NumberCheckerTest { @Test public void testIsPositive() { NumberChecker checker = new NumberChecker(); assertTrue(checker.isPositive(5)); // Missing test for negative or zero } }
Attempts:
2 left
💡 Hint
Branch coverage requires testing both true and false paths.
✗ Incorrect
The method has a branch: if number > 0 returns true, else false. The test only checks a positive number (true branch). To cover the false branch, test with a negative number and assert false. Testing zero is optional but zero is not greater than zero, so it returns false, but the key is to cover both branches.
🔧 Debug
advanced2:00remaining
Debugging incomplete branch coverage
A JUnit test suite runs but reports only 80% branch coverage for this method. What is the most likely cause?
JUnit
public class Discount { public double calculate(double price, boolean isMember) { if (isMember) { return price * 0.9; } else { return price; } } } // Test code: import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class DiscountTest { @Test public void testCalculate() { Discount d = new Discount(); assertEquals(90, d.calculate(100, true)); } }
Attempts:
2 left
💡 Hint
Branch coverage requires testing all branches of conditionals.
✗ Incorrect
The test only calls calculate with isMember = true, so only the true branch is executed. The false branch (else) is never tested, so branch coverage is less than 100%. This causes 80% coverage reported.
🧠 Conceptual
advanced2:00remaining
Difference between line and branch coverage
Which statement best describes the difference between line coverage and branch coverage in software testing?
Attempts:
2 left
💡 Hint
Think about what happens when code has if-else statements.
✗ Incorrect
Line coverage counts how many lines of code run during tests. Branch coverage counts how many possible paths (branches) in decision points like if-else are executed. Branch coverage is more detailed because it checks both true and false paths.
❓ framework
expert2:00remaining
JUnit 5 annotation for branch coverage reporting
Which JUnit 5 feature or tool integration helps generate detailed branch coverage reports automatically during test runs?
Attempts:
2 left
💡 Hint
JUnit itself does not provide coverage, but integrates with external tools.
✗ Incorrect
JUnit 5 does not have built-in annotations for coverage. Tools like JaCoCo integrate with JUnit tests via build tools (Maven, Gradle) to collect line and branch coverage data and generate reports automatically.