0
0
JUnittesting~15 mins

Why coverage measures test completeness in JUnit - Automation Benefits in Action

Choose your learning style9 modes available
Verify code coverage reflects test completeness
Preconditions (2)
Step 1: Write a simple Java class with 3 methods: add, subtract, multiply
Step 2: Write JUnit tests that call only add and subtract methods
Step 3: Run tests and generate coverage report
Step 4: Observe coverage percentage and which methods are covered
Step 5: Add a test for multiply method
Step 6: Run tests and generate coverage report again
Step 7: Compare coverage reports before and after adding multiply test
✅ Expected Result: Initial coverage report shows coverage for add and subtract only, missing multiply. After adding multiply test, coverage report shows 100% coverage of all methods, demonstrating test completeness.
Automation Requirements - JUnit 5
Assertions Needed:
Assert coverage report includes add and subtract methods after first test run
Assert coverage report includes multiply method after second test run
Assert coverage percentage increases after adding multiply test
Best Practices:
Use descriptive test method names
Use assertions to verify expected outputs
Run coverage tool automatically after tests
Keep tests independent and focused
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.*;
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;
    }

    public int multiply(int a, int b) {
        return a * b;
    }
}

public class CalculatorTest {

    Calculator calculator = new Calculator();

    @Test
    void testAdd() {
        assertEquals(5, calculator.add(2, 3));
    }

    @Test
    void testSubtract() {
        assertEquals(1, calculator.subtract(3, 2));
    }

    // Uncomment this test to increase coverage
    /*
    @Test
    void testMultiply() {
        assertEquals(6, calculator.multiply(2, 3));
    }
    */
}

/*
Instructions:
1. Run tests with only testAdd and testSubtract active.
2. Generate coverage report using Jacoco or similar.
3. Observe multiply method is not covered.
4. Uncomment testMultiply method.
5. Run tests and generate coverage report again.
6. Observe coverage is now 100%.
*/

This code defines a simple Calculator class with three methods: add, subtract, and multiply.

The test class CalculatorTest has tests for add and subtract methods initially. The multiply test is commented out.

When you run tests and generate a coverage report, only add and subtract methods are covered, so coverage is partial.

After uncommenting the multiply test and rerunning, coverage report shows all methods covered, reaching 100% coverage.

This demonstrates how coverage measures test completeness by showing which parts of code are tested.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not running coverage tool after tests', 'why_bad': "Without running coverage, you can't see which code is tested or missed.", 'correct_approach': 'Always run coverage tool after tests to get accurate coverage data.'}
Testing only some methods and assuming full coverage
Ignoring coverage reports
Bonus Challenge

Now add data-driven tests for add method with 3 different input pairs

Show Hint