0
0
JUnittesting~10 mins

Line and branch coverage in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks a simple method that returns the maximum of two numbers. It verifies both line coverage and branch coverage by testing different input cases to cover all decision paths.

Test Code - JUnit 5
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class MaxUtil {
    public static int max(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }
}

class MaxUtilTest {
    @Test
    public void testMaxFirstGreater() {
        assertEquals(5, MaxUtil.max(5, 3));
    }

    @Test
    public void testMaxSecondGreater() {
        assertEquals(7, MaxUtil.max(4, 7));
    }

    @Test
    public void testMaxEqual() {
        assertEquals(6, MaxUtil.max(6, 6));
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads MaxUtilTest classJUnit test environment initialized-PASS
2Runs testMaxFirstGreater: calls MaxUtil.max(5, 3)Inside max method with a=5, b=3Check if a > b (5 > 3) is truePASS
3Returns 5 from max methodmax method returns 5Assert returned value equals 5PASS
4Runs testMaxSecondGreater: calls MaxUtil.max(4, 7)Inside max method with a=4, b=7Check if a > b (4 > 7) is falsePASS
5Returns 7 from max methodmax method returns 7Assert returned value equals 7PASS
6Runs testMaxEqual: calls MaxUtil.max(6, 6)Inside max method with a=6, b=6Check if a > b (6 > 6) is falsePASS
7Returns 6 from max methodmax method returns 6Assert returned value equals 6PASS
8Test runner finishes all testsAll tests executedAll assertions passedPASS
Failure Scenario
Failing Condition: The max method returns incorrect value for a test case
Execution Trace Quiz - 3 Questions
Test your understanding
Which test case covers the branch where 'a > b' is true?
AtestMaxSecondGreater
BtestMaxEqual
CtestMaxFirstGreater
DNone of the above
Key Result
To achieve full branch coverage, tests must include cases for all decision outcomes, including equal values when comparing.