Bird
Raised Fist0
JUnittesting~20 mins

Line and branch coverage 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
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.

Practice

(1/5)
1. What does line coverage measure in JUnit testing?
easy
A. Whether the program runs without errors
B. Whether all possible input values are tested
C. Whether the test cases run in parallel
D. Whether each line of code is executed during tests

Solution

  1. Step 1: Understand line coverage concept

    Line coverage measures if every line of code is executed at least once during testing.
  2. Step 2: Compare with other options

    Options A, B, and D describe different testing aspects, not line coverage.
  3. Final Answer:

    Whether each line of code is executed during tests -> Option D
  4. Quick Check:

    Line coverage = executed lines [OK]
Hint: Line coverage = Did each line run? Check executed lines [OK]
Common Mistakes:
  • Confusing line coverage with branch coverage
  • Thinking line coverage checks input values
  • Assuming line coverage means no errors
2. Which of the following is the correct JUnit annotation to mark a test method for coverage measurement?
easy
A. @Test
B. @Coverage
C. @RunCoverage
D. @Measure

Solution

  1. Step 1: Recall JUnit test annotation

    The standard annotation to mark a test method in JUnit is @Test.
  2. Step 2: Verify other options

    Annotations like @Coverage, @RunCoverage, and @Measure do not exist in JUnit.
  3. Final Answer:

    @Test -> Option A
  4. Quick Check:

    JUnit test method = @Test [OK]
Hint: JUnit test methods always use @Test annotation [OK]
Common Mistakes:
  • Assuming coverage requires special annotation
  • Using non-existent annotations
  • Confusing coverage tools with JUnit annotations
3. Consider this Java method tested with JUnit:
public int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

If your tests only call max(5, 3), what coverage is achieved?
medium
A. 100% line coverage, 100% branch coverage
B. 100% line coverage, 50% branch coverage
C. 50% line coverage, 100% branch coverage
D. 50% line coverage, 50% branch coverage

Solution

  1. Step 1: Analyze lines executed by max(5, 3)

    Calling max(5, 3) executes the if condition and return a;, as well as the else block's closing brace, so all lines are executed, resulting in 100% line coverage.
  2. Step 2: Analyze branches covered

    Only the 'if' branch (a > b true) is taken; the 'else' branch is not tested, so 50% branch coverage.
  3. Final Answer:

    100% line coverage, 50% branch coverage -> Option B
  4. Quick Check:

    One path = full line coverage, partial branch coverage [OK]
Hint: Test all if-else paths for full branch coverage [OK]
Common Mistakes:
  • Thinking one test achieves 100% line coverage
  • Confusing line coverage with branch coverage
  • Ignoring else branch execution
4. You wrote tests for this method:
public boolean isPositive(int n) {
  if (n > 0) {
    return true;
  }
  return false;
}

But coverage report shows branch coverage less than 100%. What is the likely cause?
medium
A. Tests never call with n ≤ 0, missing else branch coverage
B. Tests have syntax errors causing incomplete runs
C. JUnit does not support branch coverage
D. Method has no branches to cover

Solution

  1. Step 1: Identify branches in method

    The method has two branches: if (n > 0) true, else false.
  2. Step 2: Understand branch coverage less than 100%

    If tests never call with n ≤ 0, the else branch is not executed, lowering branch coverage.
  3. Final Answer:

    Tests never call with n ≤ 0, missing else branch coverage -> Option A
  4. Quick Check:

    Branch coverage needs both if and else tested [OK]
Hint: Test both true and false conditions for full branch coverage [OK]
Common Mistakes:
  • Assuming syntax errors cause coverage issues
  • Believing JUnit lacks branch coverage support
  • Ignoring else branch execution
5. Given this method:
public int classify(int x) {
  if (x > 0) {
    if (x % 2 == 0) {
      return 1;
    } else {
      return 2;
    }
  } else {
    return 3;
  }
}

What is the minimum number of JUnit test cases needed to achieve 100% line and branch coverage?
hard
A. 4
B. 2
C. 3
D. 5

Solution

  1. Step 1: Identify branches

    There are three branches: outer if (x > 0) true and false, and inner if (x % 2 == 0) true and false.
  2. Step 2: Determine test cases to cover all branches

    Tests needed: one with x > 0 and even (return 1), one with x > 0 and odd (return 2), and one with x ≤ 0 (return 3).
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Three tests cover all branches and lines [OK]
Hint: Cover all if-else paths with minimal tests [OK]
Common Mistakes:
  • Thinking two tests cover all branches
  • Ignoring nested if branches
  • Overestimating tests needed
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