Test Overview
This test checks that a failure notification is sent when a test fails. It verifies the notification system triggers correctly on test failure.
This test checks that a failure notification is sent when a test fails. It verifies the notification system triggers correctly on test failure.
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 static org.junit.jupiter.api.Assertions.assertEquals; class FailureNotificationExtension implements TestWatcher { private static boolean notificationSent = false; @Override public void testFailed(ExtensionContext context, Throwable cause) { notificationSent = true; System.out.println("Failure notification sent for test: " + context.getDisplayName()); } public boolean isNotificationSent() { return notificationSent; } } @ExtendWith(FailureNotificationExtension.class) public class FailureNotificationTest { // Changed to static to reflect the static notificationSent flag static FailureNotificationExtension extension = new FailureNotificationExtension(); @Test void testThatFails() { assertEquals(1, 2, "This test is designed to fail"); } @Test void verifyNotificationSent() { // This test runs after testThatFails to check notification assertEquals(true, extension.isNotificationSent(), "Notification should be sent on failure"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and loads FailureNotificationTest class | JUnit test environment initialized | — | PASS |
| 2 | Runs testThatFails method | Test method executing | assertEquals(1, 2) fails | FAIL |
| 3 | FailureNotificationExtension.testFailed called with test context and failure cause | Notification flag set to true, message printed | — | PASS |
| 4 | Runs verifyNotificationSent method | Test method executing | assertEquals(true, extension.isNotificationSent()) verifies notification was sent | PASS |
public class MyTest {
@Rule
public TestWatcher watcher = new TestWatcher() {
@Override
protected void failed(Throwable e, Description d) {
System.out.println("Test failed: " + d.getMethodName());
}
};
@Test
public void testFail() {
assertEquals(1, 2);
}
}public class NotifyTest {
@Rule
public TestWatcher watcher = new TestWatcher() {
@Override
protected void failed() {
System.out.println("Failure detected");
}
};
@Test
public void testExample() {
fail();
}
}public class EmailAlertTest {
@Rule
public TestWatcher watcher = new TestWatcher() {
@Override
protected void failed(Throwable e, Description d) {
// What should go here?
}
};
@Test
public void testEmail() {
assertTrue(false);
}
}Pass the test and this pattern moves unknown