0
0
JUnittesting~10 mins

Coverage thresholds in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs unit tests and checks if the code coverage meets the minimum required thresholds for instructions, branches, and lines. It verifies that the coverage is above the set limits to ensure good test quality.

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

public class CoverageThresholdTest {

    @Test
    void sampleTest() {
        int result = add(2, 3);
        assertEquals(5, result);
    }

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

    // Note: Coverage thresholds are usually configured in build tools like Maven or Gradle,
    // but here we simulate a check by asserting coverage values.

    @Test
    void coverageThresholdCheck() {
        // Simulated coverage values (in percent)
        int instructionCoverage = 85;
        int branchCoverage = 80;
        int lineCoverage = 90;

        int minInstructionCoverage = 80;
        int minBranchCoverage = 75;
        int minLineCoverage = 85;

        assertTrue(instructionCoverage >= minInstructionCoverage, "Instruction coverage below threshold");
        assertTrue(branchCoverage >= minBranchCoverage, "Branch coverage below threshold");
        assertTrue(lineCoverage >= minLineCoverage, "Line coverage below threshold");
    }
}
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads CoverageThresholdTest classJUnit test environment initialized-PASS
2Runs sampleTest methodMethod add(2,3) returns 5assertEquals(5, result)PASS
3Runs coverageThresholdCheck methodSimulated coverage values: instruction=85%, branch=80%, line=90%assertTrue(instructionCoverage >= 80), assertTrue(branchCoverage >= 75), assertTrue(lineCoverage >= 85)PASS
4Test runner completes all testsAll tests passed-PASS
Failure Scenario
Failing Condition: One or more coverage values are below the minimum thresholds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the coverageThresholdCheck test verify?
AThat the test runner starts correctly
BThat the add method returns correct sums
CThat code coverage percentages meet minimum required thresholds
DThat the coverage tool is installed
Key Result
Setting coverage thresholds helps maintain good test quality by ensuring enough code is tested, preventing unnoticed bugs.