Failure notification setup in JUnit - Build an Automation Script
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.mockito.Mockito.*; import java.util.Optional; class NotificationService { void sendFailureNotification(String testName, String reason) { // Imagine this sends an email or logs the failure } } class FailureNotificationWatcher implements TestWatcher { private final NotificationService notificationService; FailureNotificationWatcher(NotificationService notificationService) { this.notificationService = notificationService; } @Override public void testFailed(ExtensionContext context, Throwable cause) { String testName = context.getDisplayName(); String reason = cause.getMessage(); notificationService.sendFailureNotification(testName, reason); } @Override public void testSuccessful(ExtensionContext context) { // No notification on success } } @ExtendWith(FailureNotificationWatcher.class) public class FailureNotificationTest { static NotificationService notificationService = mock(NotificationService.class); static FailureNotificationWatcher watcher = new FailureNotificationWatcher(notificationService); @Test void passingTest() { // This test passes assert(true); } @Test void failingTest() { // This test fails throw new RuntimeException("Simulated failure"); } @Test void verifyNotificationSent() { // Run the failing test manually to trigger notification ExtensionContext context = mock(ExtensionContext.class); when(context.getDisplayName()).thenReturn("failingTest()"); RuntimeException failure = new RuntimeException("Simulated failure"); watcher.testFailed(context, failure); verify(notificationService, times(1)).sendFailureNotification("failingTest()", "Simulated failure"); } }
The NotificationService class simulates sending failure notifications.
The FailureNotificationWatcher implements JUnit 5's TestWatcher interface to listen for test failures. When a test fails, it calls sendFailureNotification with the test name and failure reason.
The test class FailureNotificationTest uses Mockito to mock NotificationService. It defines a passing test and a failing test. The verifyNotificationSent test manually triggers the failure notification and verifies that the notification method was called exactly once with the correct parameters.
This setup ensures that failure notifications are sent only on test failures and contain the right information.
Now add data-driven testing with 3 different failing scenarios and verify notifications for each