刘嘉昕 | 92bb290 | 2025-06-09 10:35:27 +0800 | [diff] [blame^] | 1 | package com.pt5.pthouduan.ControllerTest; |
| 2 | |
| 3 | |
| 4 | import com.pt5.pthouduan.controller.TrafficController; |
| 5 | import com.pt5.pthouduan.entity.UserTrafficStat; |
| 6 | import com.pt5.pthouduan.mapper.UserTrafficMapper; |
| 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.time.LocalDate; |
| 17 | |
| 18 | import static org.mockito.ArgumentMatchers.anyString; |
| 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.Mockito.when; |
| 21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 22 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 23 | |
| 24 | class TrafficControllerTest { |
| 25 | |
| 26 | private MockMvc mockMvc; |
| 27 | |
| 28 | @Mock |
| 29 | private UserTrafficMapper userTrafficMapper; // 模拟 Mapper |
| 30 | |
| 31 | @InjectMocks |
| 32 | private TrafficController trafficController; // 注入 Controller |
| 33 | |
| 34 | @BeforeEach |
| 35 | void setUp() { |
| 36 | MockitoAnnotations.openMocks(this); // 初始化 Mock |
| 37 | mockMvc = MockMvcBuilders.standaloneSetup(trafficController).build(); // 构建 MockMvc |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | void getUserTrafficStats_shouldReturnStats_whenPasskeyAndDatesAreValid() throws Exception { |
| 42 | // 准备测试数据 |
| 43 | String passkey = "test-passkey"; |
| 44 | LocalDate startDate = LocalDate.of(2023, 1, 1); |
| 45 | LocalDate endDate = LocalDate.of(2023, 1, 31); |
| 46 | |
| 47 | UserTrafficStat mockStats = new UserTrafficStat(); |
| 48 | mockStats.setTotalUploaded(1024L); |
| 49 | mockStats.setTotalDownloaded(2048L); |
| 50 | |
| 51 | // 模拟 Mapper 行为 |
| 52 | when(userTrafficMapper.getUserTrafficStats(anyString(), any(LocalDate.class), any(LocalDate.class))) |
| 53 | .thenReturn(mockStats); |
| 54 | |
| 55 | // 发起 GET 请求并验证响应 |
| 56 | mockMvc.perform(get("/api/traffic/user-stats") |
| 57 | .param("passkey", passkey) |
| 58 | .param("startDate", startDate.toString()) |
| 59 | .param("endDate", endDate.toString()) |
| 60 | .contentType(MediaType.APPLICATION_JSON)) |
| 61 | .andExpect(status().isOk()); // 预期 HTTP 200 |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | void getUserTrafficStats_shouldReturn400_whenPasskeyIsMissing() throws Exception { |
| 66 | // 不传 passkey,预期返回 400 Bad Request |
| 67 | mockMvc.perform(get("/api/traffic/user-stats") |
| 68 | .param("startDate", "2023-01-01") |
| 69 | .param("endDate", "2023-01-31") |
| 70 | .contentType(MediaType.APPLICATION_JSON)) |
| 71 | .andExpect(status().isBadRequest()); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | void getUserTrafficStats_shouldReturn400_whenDatesAreInvalid() throws Exception { |
| 76 | // 传入无效日期格式,预期返回 400 Bad Request |
| 77 | mockMvc.perform(get("/api/traffic/user-stats") |
| 78 | .param("passkey", "test-passkey") |
| 79 | .param("startDate", "2023-01-01-invalid") |
| 80 | .param("endDate", "2023-01-31") |
| 81 | .contentType(MediaType.APPLICATION_JSON)) |
| 82 | .andExpect(status().isBadRequest()); |
| 83 | } |
| 84 | } |