Test Overview
This test uses JUnit's TestWatcher to report test success and failure events. It verifies that the test lifecycle events are logged correctly.
This test uses JUnit's TestWatcher to report test success and failure events. It verifies that the test lifecycle events are logged correctly.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.TestWatcher; import org.junit.jupiter.api.extension.ExtensionContext; import java.util.Optional; class ReportingTestWatcher implements TestWatcher { @Override public void testSuccessful(ExtensionContext context) { System.out.println("Test succeeded: " + context.getDisplayName()); } @Override public void testFailed(ExtensionContext context, Throwable cause) { System.out.println("Test failed: " + context.getDisplayName() + ", reason: " + cause.getMessage()); } @Override public void testDisabled(ExtensionContext context, Optional<String> reason) { System.out.println("Test disabled: " + context.getDisplayName() + ", reason: " + reason.orElse("No reason")); } @Override public void testAborted(ExtensionContext context, Throwable cause) { System.out.println("Test aborted: " + context.getDisplayName() + ", reason: " + cause.getMessage()); } } @ExtendWith(ReportingTestWatcher.class) public class SampleTest { @Test void testSuccess() { // This test will pass int sum = 2 + 3; org.junit.jupiter.api.Assertions.assertEquals(5, sum); } @Test void testFailure() { // This test will fail int diff = 5 - 3; org.junit.jupiter.api.Assertions.assertEquals(1, diff); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and applies ReportingTestWatcher extension | JUnit test environment initialized with TestWatcher extension | — | PASS |
| 2 | Runs testSuccess method | Executing testSuccess: calculates sum 2 + 3 | Assert sum equals 5 | PASS |
| 3 | TestWatcher logs success message for testSuccess | Console output: 'Test succeeded: testSuccess()' | Verify success log message printed | PASS |
| 4 | Runs testFailure method | Executing testFailure: calculates diff 5 - 3 | Assert diff equals 1 | FAIL |
| 5 | TestWatcher logs failure message for testFailure | Console output: 'Test failed: testFailure(), reason: expected:<1> but was:<2>' | Verify failure log message printed with correct reason | PASS |
TestWatcher in JUnit tests?TestWatcher is a JUnit class designed to listen to test lifecycle events such as when a test starts, succeeds, fails, or finishes.TestWatcher.TestWatcher method that runs when a test fails?failed method in TestWatcher is public void failed(ExtensionContext context, Throwable cause).ExtensionContext first, then Throwable.TestWatcher subclass, what will be printed if the test exampleTest fails?class MyWatcher extends TestWatcher {
@Override
public void failed(ExtensionContext context, Throwable cause) {
context.getTestMethod().ifPresent(method -> System.out.println(method.getName() + " failed"));
}
}
@Test
@ExtendWith(MyWatcher.class)
void exampleTest() {
assertEquals(1, 2);
}failed method prints the test method name plus " failed".exampleTest, so the output will be "exampleTest failed".TestWatcher subclass that aims to log test success:class SuccessWatcher extends TestWatcher {
@Override
public void succeeded(Description description) {
System.out.println(description.getDisplayName() + " succeeded");
}
}TestWatcher methods use ExtensionContext parameter, not Description.public void succeeded(ExtensionContext context) in JUnit 5.TestWatcher that logs test start and failure events, but only for tests whose names start with "critical". Which approach correctly implements this behavior in JUnit 5?TestWatcher methods to override are starting(ExtensionContext) and failed(ExtensionContext, Throwable).context.getDisplayName() to get the test name and check if it starts with "critical" before logging.starting(ExtensionContext context) and failed(ExtensionContext context, Throwable cause), check context.getDisplayName() starts with "critical" before logging [OK]Pass the test and this pattern moves unknown