Назад к вопросам
Senior
7
Напишите тесты к системе фильтрации уведомлений с использованием JUnit 5 и Mockito, покрывающие основные сценарии: null/пустой ввод, валидное уведомление, блокировка отправителя, фильтрация по каналу, дедупликация.
Компании, где спрашивали
Яндекс
Ответ от нейросети
sobes.tech AI
Ниже пример тестов для системы фильтрации уведомлений с использованием JUnit 5 и Mockito, покрывающих основные сценарии:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class NotificationFilterTest {
private NotificationFilter filter;
private SenderBlocklistService blocklistService;
private DeduplicationService dedupService;
@BeforeEach
void setUp() {
blocklistService = mock(SenderBlocklistService.class);
dedupService = mock(DeduplicationService.class);
filter = new NotificationFilter(blocklistService, dedupService);
}
@Test
void testNullInput() {
assertThrows(IllegalArgumentException.class, () -> filter.filter(null));
}
@Test
void testEmptyInput() {
Notification emptyNotification = new Notification("");
assertFalse(filter.filter(emptyNotification));
}
@Test
void testValidNotification() {
Notification notification = new Notification("valid message");
when(blocklistService.isBlocked(anyString())).thenReturn(false);
when(dedupService.isDuplicate(any())).thenReturn(false);
boolean result = filter.filter(notification);
assertTrue(result);
}
@Test
void testBlockedSender() {
Notification notification = new Notification("message from blocked sender");
when(blocklistService.isBlocked(anyString())).thenReturn(true);
boolean result = filter.filter(notification);
assertFalse(result);
}
@Test
void testFilterByChannel() {
Notification notification = new Notification("message");
notification.setChannel("email");
filter.setAllowedChannels(Set.of("sms", "push"));
boolean result = filter.filter(notification);
assertFalse(result);
}
@Test
void testDeduplication() {
Notification notification = new Notification("duplicate message");
when(blocklistService.isBlocked(anyString())).thenReturn(false);
when(dedupService.isDuplicate(notification)).thenReturn(true);
boolean result = filter.filter(notification);
assertFalse(result);
}
}
В этом примере:
NotificationFilter— класс фильтрации уведомлений.SenderBlocklistService— сервис проверки заблокированных отправителей.DeduplicationService— сервис для дедупликации уведомлений.
Тесты покрывают:
- null и пустой ввод,
- валидное уведомление,
- блокировку отправителя,
- фильтрацию по каналу,
- дедупликацию.