Test result publishing in JUnit - Build an Automation Script
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.
Now add data-driven testing with 3 different input sets for a calculator add method
Practice
Solution
Step 1: Understand the purpose of test result publishing
Test result publishing shows the outcome of running tests, mainly if they pass or fail.Step 2: Identify what is displayed after tests run
JUnit displays test results to inform developers about test success or failure.Final Answer:
Whether tests pass or fail -> Option AQuick Check:
Test result publishing = pass/fail status [OK]
- Confusing test results with source code
- Thinking test results show test writing time
- Assuming test results count files
Solution
Step 1: Identify the annotation that marks test methods
The @Test annotation tells JUnit which methods to run as tests and publish results for.Step 2: Differentiate from other lifecycle annotations
@BeforeAll and @AfterEach are for setup/cleanup, @Ignore skips tests, so they don't mark tests themselves.Final Answer:
@Test -> Option AQuick Check:
Test method annotation = @Test [OK]
- Using @BeforeAll or @AfterEach as test markers
- Confusing @Ignore with test execution
- Missing @Test annotation on test methods
@Test
void testSum() {
assertEquals(5, 2 + 3);
}Solution
Step 1: Evaluate the assertion in the test method
The assertion checks if 2 + 3 equals 5, which is true.Step 2: Determine the test outcome based on assertion
Since the assertion is true, the test passes and JUnit publishes a pass result.Final Answer:
Test passes because 2 + 3 equals 5 -> Option CQuick Check:
2 + 3 = 5 means test passes [OK]
- Assuming 2 + 3 is not 5
- Thinking missing annotation causes ignore here
- Confusing syntax errors with assertion failures
@Test
void testEmptyList() {
List<String> list = new ArrayList<>();
assertTrue(list.isEmpty());
}Solution
Step 1: Analyze the list initialization
The list is created as a new empty ArrayList, so list.isEmpty() returns true.Step 2: Check the assertion logic
assertTrue(list.isEmpty()) expects true, which matches the empty list state, so the test passes.Final Answer:
No error; test should pass and publish success -> Option BQuick Check:
Empty list means assertTrue(list.isEmpty()) passes [OK]
- Thinking list is not empty after initialization
- Confusing assertTrue with assertFalse
- Assuming missing imports cause test failure without error
Solution
Step 1: Understand automated test result publishing
Build tools like Maven or Gradle integrate with JUnit to run tests and generate reports automatically.Step 2: Evaluate other options for report generation
Manually writing or copying results is error-prone and inefficient; @Report annotation does not exist in JUnit.Final Answer:
Use a build tool like Maven or Gradle with JUnit plugins to generate reports -> Option DQuick Check:
Automate reports with build tools [OK]
- Trying to write results manually inside tests
- Copying console output instead of automating
- Assuming non-existent annotations generate reports
Ready to prove it?
Pass the test and this pattern moves unknown
