0
0
JUnittesting~10 mins

Coverage reports in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs a simple JUnit test case and generates a coverage report to verify which parts of the code were executed during the test.

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

class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }
}

class CalculatorTest {
    @Test
    public void testAdd() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.add(2, 3));
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads CalculatorTest classJUnit test environment initialized-PASS
2JUnit executes testAdd() methodCalculator instance created, add method called with (2,3)assertEquals(5, calc.add(2, 3)) verifies result is 5PASS
3Test completes successfullyTest method finished without exceptions-PASS
4Coverage tool analyzes which methods were executedCoverage data collected for add() method, subtract() method not executedCoverage report shows add() method covered, subtract() method not coveredPASS
5Coverage report generated and savedReport file available with coverage summaryReport confirms 50% method coverage for Calculator classPASS
Failure Scenario
Failing Condition: TestAdd method fails assertion or coverage tool fails to generate report
Execution Trace Quiz - 3 Questions
Test your understanding
What does the coverage report indicate after running the testAdd() method?
ANo methods were covered
BOnly the add() method was executed and covered
CBoth add() and subtract() methods were covered
DOnly subtract() method was covered
Key Result
Generating coverage reports helps identify which parts of the code are tested and which are not, guiding better test coverage and quality.