Line coverage measures the percentage of code lines executed by tests. It shows which lines ran during testing.
Click to reveal answer
beginner
Define branch coverage.
Branch coverage checks if each possible path (true/false) in decision points like if-else statements is tested.
Click to reveal answer
intermediate
Why is branch coverage important beyond line coverage?
Branch coverage ensures all decision outcomes are tested, catching bugs missed by line coverage alone.
Click to reveal answer
intermediate
Example: What does 100% line coverage but 50% branch coverage mean?
All lines ran, but only half of decision paths (branches) were tested. Some conditions may be untested.
Click to reveal answer
beginner
How can JUnit tests help improve branch coverage?
By writing tests that cover all if-else outcomes and loops, JUnit tests increase branch coverage.
Click to reveal answer
What does line coverage measure?
AExecution time of tests
BNumber of test cases written
CNumber of bugs found
DPercentage of executed code lines
✗ Incorrect
Line coverage shows how many lines of code ran during testing.
Branch coverage ensures testing of:
AAll decision outcomes (true/false)
BAll variables
CAll lines of code
DAll classes
✗ Incorrect
Branch coverage tests all possible paths in decision points.
If a test covers every line but misses some branches, coverage is:
A100% line, 100% branch
B100% line, less than 100% branch
CLess than 100% line, 100% branch
DLess than 100% line and branch
✗ Incorrect
All lines ran but some decision paths were not tested.
Which JUnit feature helps check coverage?
AAssertions
BAnnotations only
CTest runners with coverage tools
DException handling
✗ Incorrect
JUnit combined with coverage tools measures line and branch coverage.
Branch coverage is especially useful for testing:
ALoops and conditions
BComments
CVariable declarations
DClass names
✗ Incorrect
Branch coverage ensures all loop and condition paths are tested.
Explain the difference between line coverage and branch coverage.
Think about code lines vs decision points.
You got /3 concepts.
How can you write JUnit tests to improve branch coverage?
Consider all possible paths in your code.
You got /3 concepts.
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
Step 1: Understand line coverage concept
Line coverage measures if every line of code is executed at least once during testing.
Step 2: Compare with other options
Options A, B, and D describe different testing aspects, not line coverage.
Final Answer:
Whether each line of code is executed during tests -> Option D
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
Step 1: Recall JUnit test annotation
The standard annotation to mark a test method in JUnit is @Test.
Step 2: Verify other options
Annotations like @Coverage, @RunCoverage, and @Measure do not exist in JUnit.
Final Answer:
@Test -> Option A
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
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.
Step 2: Analyze branches covered
Only the 'if' branch (a > b true) is taken; the 'else' branch is not tested, so 50% branch coverage.
Final Answer:
100% line coverage, 50% branch coverage -> Option B
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
Step 1: Identify branches in method
The method has two branches: if (n > 0) true, else false.
Step 2: Understand branch coverage less than 100%
If tests never call with n ≤ 0, the else branch is not executed, lowering branch coverage.
Final Answer:
Tests never call with n ≤ 0, missing else branch coverage -> Option A
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
Step 1: Identify branches
There are three branches: outer if (x > 0) true and false, and inner if (x % 2 == 0) true and false.
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).
Final Answer:
3 -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.