0
0
JUnittesting~15 mins

Coverage reports in JUnit - Build an Automation Script

Choose your learning style9 modes available
Generate and verify code coverage report for Calculator class
Preconditions (3)
Step 1: Run the CalculatorTest JUnit test class using the build tool
Step 2: Locate the generated JaCoCo coverage report in the target or build directory
Step 3: Open the coverage report HTML file in a browser
Step 4: Verify that the coverage report shows 100% coverage for Calculator class methods add and subtract
✅ Expected Result: The coverage report shows 100% line and branch coverage for the Calculator class methods tested
Automation Requirements - JUnit 5 with JaCoCo
Assertions Needed:
Test methods execute without errors
Coverage report file exists after test run
Coverage report indicates 100% coverage for Calculator class methods
Best Practices:
Use JUnit 5 annotations and assertions
Configure JaCoCo plugin properly in build tool
Run tests and generate coverage report in a single build command
Verify coverage report file existence programmatically
Do not hardcode file paths; use relative paths or build tool properties
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.io.File;

public class CalculatorTest {

    @Test
    public void testAdd() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.add(2, 3));
        assertEquals(-1, calc.add(2, -3));
    }

    @Test
    public void testSubtract() {
        Calculator calc = new Calculator();
        assertEquals(1, calc.subtract(3, 2));
        assertEquals(5, calc.subtract(2, -3));
    }

    @Test
    public void testCoverageReportExists() {
        // Assuming Maven default target directory and JaCoCo report path
        File report = new File("target/site/jacoco/index.html");
        assertTrue(report.exists(), "Coverage report should exist after tests run");
    }
}

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

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

/*
Note: To generate coverage report, run:
  mvn clean test jacoco:report

Then open target/site/jacoco/index.html to verify coverage manually.
*/

The test class CalculatorTest contains three tests:

  • testAdd() verifies the add method with two cases.
  • testSubtract() verifies the subtract method with two cases.
  • testCoverageReportExists() checks if the JaCoCo coverage report file exists after running tests.

The Calculator class is simple with two methods to test.

Running mvn clean test jacoco:report will execute tests and generate the coverage report.

The coverage report file existence is asserted programmatically to confirm report generation.

Manual verification of coverage percentage is recommended by opening the HTML report in a browser.

Common Mistakes - 4 Pitfalls
Not configuring JaCoCo plugin in the build tool
Hardcoding absolute file paths for coverage report
Not running the coverage report generation goal after tests
Not asserting coverage report existence in automation
Bonus Challenge

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

Show Hint