0
0
JUnittesting~15 mins

JaCoCo setup and configuration in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify JaCoCo code coverage report generation after running JUnit tests
Preconditions (3)
Step 1: Run 'mvn clean test' command in the project root directory
Step 2: Locate the generated JaCoCo report in 'target/site/jacoco/index.html'
Step 3: Open the 'index.html' report in a browser
Step 4: Verify that the report shows coverage metrics for the tested classes
✅ Expected Result: JaCoCo generates a coverage report showing coverage percentages for classes tested by JUnit tests
Automation Requirements - JUnit 5 with Maven
Assertions Needed:
Verify that the JaCoCo report file 'target/site/jacoco/index.html' exists after tests run
Verify that the coverage report contains coverage data for at least one class
Best Practices:
Use Maven lifecycle to run tests and generate reports
Check file existence and content programmatically
Keep test code and coverage configuration separate
Use assertions to validate report generation
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

public class JaCoCoReportTest {

    @Test
    public void testJaCoCoReportExistsAndHasContent() throws Exception {
        // Path to the JaCoCo report
        Path reportPath = Paths.get("target/site/jacoco/index.html");

        // Assert the report file exists
        assertTrue(Files.exists(reportPath), "JaCoCo report file should exist");

        // Read report content
        String content = Files.readString(reportPath);

        // Assert the report contains coverage data (check for a known marker like 'Coverage Summary')
        assertTrue(content.contains("Coverage Summary") || content.contains("class coverage"),
                "JaCoCo report should contain coverage summary");
    }
}

This JUnit 5 test checks if the JaCoCo coverage report is generated after running tests.

First, it verifies the report file target/site/jacoco/index.html exists. This confirms the report was created.

Then, it reads the report content as a string and checks for a known phrase like Coverage Summary to ensure the report contains coverage data.

This approach uses standard Java file APIs and JUnit assertions to validate the presence and content of the coverage report, following best practices for automation.

Common Mistakes - 3 Pitfalls
{'mistake': "Not running 'mvn test' before checking the report", 'why_bad': 'The JaCoCo report is generated only after tests run, so checking before running tests will fail.', 'correct_approach': "Always run 'mvn clean test' or equivalent to generate the report before verifying it."}
{'mistake': 'Hardcoding absolute file paths for the report', 'why_bad': 'Absolute paths reduce portability and cause failures on different machines or environments.', 'correct_approach': "Use relative paths like 'target/site/jacoco/index.html' relative to the project root."}
Checking only file existence without validating content
Bonus Challenge

Now add data-driven testing to verify JaCoCo reports for multiple Maven modules with different paths

Show Hint