0
0
JUnittesting~15 mins

Test result publishing in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify test result publishing after test execution
Preconditions (2)
Step 1: Run the JUnit test suite using the build tool or IDE
Step 2: Locate the test result report generated (e.g., in target/surefire-reports or build/reports/tests)
Step 3: Open the test result report file (e.g., XML or HTML)
Step 4: Verify that the report contains the executed test names
Step 5: Verify that the report shows the correct pass/fail status for each test
✅ Expected Result: The test result report is generated and correctly shows all executed tests with their pass or fail status.
Automation Requirements - JUnit 5
Assertions Needed:
Verify test methods run and pass/fail as expected
Verify test result report file exists after test execution
Best Practices:
Use @Test annotations for test methods
Use assertions from org.junit.jupiter.api.Assertions
Run tests via Maven or Gradle to generate reports
Do not hardcode file paths; use relative paths
Keep tests independent and repeatable
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Files;
import java.nio.file.Path;

public class SampleTest {

    @Test
    void testAddition() {
        int sum = 2 + 3;
        assertEquals(5, sum, "2 + 3 should equal 5");
    }

    @Test
    void testSubtraction() {
        int diff = 5 - 3;
        assertEquals(2, diff, "5 - 3 should equal 2");
    }

    @Test
    void testFailingCase() {
        assertTrue(false, "This test is designed to fail");
    }

    @Test
    void verifyReportExists() throws Exception {
        // Assuming Maven default surefire report location
        Path reportPath = Path.of("target", "surefire-reports", "TEST-SampleTest.xml");
        assertTrue(Files.exists(reportPath), "Test report file should exist after tests run");
    }
}

This JUnit 5 test class SampleTest contains three test methods: two that pass and one that fails to demonstrate test result variety.

The verifyReportExists test checks if the test report file is generated in the default Maven Surefire reports folder. This confirms that test results are published after execution.

Assertions use JUnit's assertEquals and assertTrue for clarity. The report path uses Path.of with relative folders to avoid hardcoding absolute paths.

Run these tests via Maven or Gradle to generate the reports automatically. The failing test shows how failures appear in reports.

Common Mistakes - 4 Pitfalls
Not running tests via build tool, so no report is generated
Hardcoding absolute file paths for report verification
{'mistake': 'Not including assertions to verify test outcomes', 'why_bad': "Without assertions, tests may pass incorrectly and reports won't reflect real test results", 'correct_approach': 'Always include meaningful assertions in test methods'}
Ignoring test failures and assuming all tests pass
Bonus Challenge

Now add data-driven testing with 3 different input sets for a calculator add method

Show Hint