0
0
JUnittesting~10 mins

Why coverage measures test completeness in JUnit - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks if all methods in a simple Calculator class are called during testing. It verifies test completeness by measuring code coverage.

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

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

public class CalculatorTest {
    @Test
    void testAdd() {
        Calculator calc = new Calculator();
        int result = calc.add(5, 3);
        assertEquals(8, result);
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initializes-PASS
2CalculatorTest.testAdd() method is calledCalculator instance created-PASS
3Calculator.add(5, 3) method is calledInside add method, parameters a=5, b=3-PASS
4add method returns 8Return value 8 passed back to testAdd-PASS
5testAdd asserts result equals 8Assertion compares expected 8 with actual 8assertEquals(8, result)PASS
6Test endsTest runner completes testAdd-PASS
7Coverage report generatedReport shows add method covered, subtract method not coveredCoverage measures which methods ran during testsPASS
Failure Scenario
Failing Condition: If the add method is not called in any test
Execution Trace Quiz - 3 Questions
Test your understanding
What does code coverage measure in this test?
AThe number of assertions in the test
BHow fast the test runs
CWhich methods were executed during testing
DThe memory used by the test
Key Result
Measuring code coverage helps ensure tests run all important parts of the code, showing test completeness.