Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
| 3 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.g9.g9backend.pojo.Notification; |
| 6 | import com.g9.g9backend.service.NotificationService; |
| 7 | import org.junit.jupiter.api.BeforeEach; |
| 8 | import org.junit.jupiter.api.Test; |
| 9 | import org.mockito.InjectMocks; |
| 10 | import org.mockito.Mock; |
| 11 | import org.mockito.MockitoAnnotations; |
| 12 | import org.springframework.http.MediaType; |
| 13 | import org.springframework.test.web.servlet.MockMvc; |
| 14 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 15 | |
| 16 | import java.util.Date; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.Mockito.*; |
| 21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 22 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 23 | |
| 24 | public 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 | } |