0
0
JUnittesting~20 mins

Line and branch coverage 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
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));
    }
}
A75%
B50%
C25%
D100%
Attempts:
2 left
💡 Hint
Only the add method is tested, subtract is not called.
assertion
intermediate
2: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
    }
}
AAdd assertFalse(checker.isPositive(-1)); to test negative number
BAdd assertTrue(checker.isPositive(0)); to test zero
CAdd assertFalse(checker.isPositive(1)); to test positive number
DAdd assertTrue(checker.isPositive(-5)); to test negative number
Attempts:
2 left
💡 Hint
Branch coverage requires testing both true and false paths.
🔧 Debug
advanced
2: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));
    }
}
ATest only covers isMember = true branch, missing isMember = false branch
BTest has wrong expected value causing assertion failure
CMethod has syntax error causing partial coverage
DTest does not instantiate Discount class properly
Attempts:
2 left
💡 Hint
Branch coverage requires testing all branches of conditionals.
🧠 Conceptual
advanced
2:00remaining
Difference between line and branch coverage
Which statement best describes the difference between line coverage and branch coverage in software testing?
ALine coverage measures number of tests; branch coverage measures number of bugs
BLine coverage measures executed branches; branch coverage measures executed lines
CLine coverage measures executed lines; branch coverage measures executed decision outcomes
DLine coverage measures code complexity; branch coverage measures test speed
Attempts:
2 left
💡 Hint
Think about what happens when code has if-else statements.
framework
expert
2: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?
AUse @TestCoverage annotation to enable branch coverage
BUse @BranchCoverage annotation in JUnit 5 tests
CUse JUnit 5 built-in branch coverage reporter with @CoverageReport
DUse JaCoCo agent with Maven Surefire plugin to collect coverage data
Attempts:
2 left
💡 Hint
JUnit itself does not provide coverage, but integrates with external tools.