-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationServiceTest.java
More file actions
55 lines (44 loc) · 2.06 KB
/
Copy pathNotificationServiceTest.java
File metadata and controls
55 lines (44 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.techevents.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.techevents.model.Event;
import com.techevents.model.Subscriber;
import com.techevents.model.NotificationMessage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.test.util.ReflectionTestUtils;
import java.time.LocalDate;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
class NotificationServiceTest {
private RabbitTemplate rabbitTemplate;
private ObjectMapper objectMapper;
private NotificationService notificationService;
@BeforeEach
void setUp() {
rabbitTemplate = mock(RabbitTemplate.class);
objectMapper = new ObjectMapper();
notificationService = new NotificationService(rabbitTemplate, objectMapper);
// Inject config values manually
ReflectionTestUtils.setField(notificationService, "exchange", "notifications.exchange");
ReflectionTestUtils.setField(notificationService, "routingKey", "notifications.event.created");
}
@Test
void sendNotification_shouldPublishToRabbitMQ_withCorrectPayload() {
// Given
Event event = new Event("Spring Boot Conf", "Great talk", LocalDate.of(2025, 6, 1), "Boston", List.of("spring"));
Subscriber subscriber = new Subscriber("user@example.com");
// When
notificationService.sendNotification(subscriber, event);
// Then
ArgumentCaptor<String> payloadCaptor = ArgumentCaptor.forClass(String.class);
verify(rabbitTemplate).convertAndSend(eq("notifications.exchange"), eq("notifications.event.created"), payloadCaptor.capture());
String payload = payloadCaptor.getValue();
assertTrue(payload.contains("user@example.com"));
assertTrue(payload.contains("Spring Boot Conf"));
assertTrue(payload.contains("2025-06-01"));
assertTrue(payload.contains("Boston"));
}
}