0
0
JUnittesting~15 mins

Failure notification setup in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify failure notification is sent when a test fails
Preconditions (2)
Step 1: Create a JUnit test class with one passing test and one failing test
Step 2: Run the test suite
Step 3: Observe the failure notification system for messages about the failing test
✅ Expected Result: A notification is sent only for the failing test, containing the test name and failure reason
Automation Requirements - JUnit 5
Assertions Needed:
Verify that the failure notification method is called exactly once for the failing test
Verify the notification contains the correct test name and failure message
Best Practices:
Use JUnit 5 TestWatcher extension to listen for test failures
Use mock objects or spies to verify notification calls
Keep test code clean and readable
Use descriptive test method names
Automated Solution
JUnit
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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not using a TestWatcher or listener and trying to send notifications inside test methods', 'why_bad': 'This mixes test logic with notification logic and is not scalable or maintainable.', 'correct_approach': "Use JUnit 5's TestWatcher extension to separate notification logic from test code."}
Hardcoding notification calls without mocking in tests
Not verifying the notification content (test name and failure reason)
Bonus Challenge

Now add data-driven testing with 3 different failing scenarios and verify notifications for each

Show Hint