blob: 121258f6c85f4ccaad67ae69f3849dbe62212b2b [file] [log] [blame]
Seamher176d3312025-06-06 16:57:34 +08001package com.g9.g9backend.controller;
2
3import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.g9.g9backend.pojo.Notification;
6import com.g9.g9backend.service.NotificationService;
7import org.junit.jupiter.api.BeforeEach;
8import org.junit.jupiter.api.Test;
9import org.mockito.InjectMocks;
10import org.mockito.Mock;
11import org.mockito.MockitoAnnotations;
12import org.springframework.http.MediaType;
13import org.springframework.test.web.servlet.MockMvc;
14import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15
16import java.util.Date;
17import java.util.List;
18
19import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.Mockito.*;
21import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
22import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
23
24public class NotificationControllerTest {
25
26 private MockMvc mockMvc;
27
28 @InjectMocks
29 private NotificationController notificationController;
30
31 @Mock
32 private NotificationService notificationService;
33
34 private final ObjectMapper objectMapper = new ObjectMapper();
35
36 @BeforeEach
37 public void setup() {
38 MockitoAnnotations.openMocks(this);
39 mockMvc = MockMvcBuilders.standaloneSetup(notificationController).build();
40 }
41
42 // ✅ 测试标记已读
43 @Test
44 public void testGetNotificationRead_success() throws Exception {
45 Notification notification = new Notification();
46 notification.setNotificationId(1);
47
48 Notification updatedNotification = new Notification();
49 updatedNotification.setNotificationId(1);
50 updatedNotification.setRead(false);
51
52 when(notificationService.getById(1)).thenReturn(updatedNotification);
53 when(notificationService.updateById(any())).thenReturn(true);
54
55 mockMvc.perform(post("/notification/read")
56 .contentType(MediaType.APPLICATION_JSON)
57 .content(objectMapper.writeValueAsString(notification)))
58 .andExpect(status().isOk());
59 }
60
61 // ✅ 测试删除通知
62 @Test
63 public void testDeleteNotification_success() throws Exception {
64 when(notificationService.removeById(1)).thenReturn(true);
65
66 mockMvc.perform(delete("/notification")
67 .param("notificationId", "1"))
68 .andExpect(status().isNoContent());
69 }
70
71 // ✅ 测试分页获取通知
72 @Test
73 public void testGetNotification_success() throws Exception {
74 Notification notification = new Notification();
75 notification.setNotificationId(1);
76 notification.setUserId(1);
77 notification.setTitle("title");
78 notification.setContent("content");
79 notification.setCreateAt(new Date());
80 notification.setRead(false);
81 notification.setTriggeredBy(2);
82 notification.setRelatedId(3);
83
84 Page<Notification> page = new Page<>(1, 10);
85 page.setRecords(List.of(notification));
86 page.setTotal(1);
87
88 when(notificationService.page(any(), any())).thenReturn(page);
89
90 mockMvc.perform(get("/notification")
91 .param("userId", "1")
92 .param("pageNumber", "1")
93 .param("rows", "10"))
94 .andExpect(status().isOk())
95 .andExpect(jsonPath("$.records[0].notificationId").value(1))
96 .andExpect(jsonPath("$.records[0].title").value("title"))
97 .andExpect(jsonPath("$.total").value(1));
98 }
99}