Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 1 | package com.pt5.pthouduan.ControllerTest; |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 2 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 3 | import com.pt5.pthouduan.controller.UserController; |
| 4 | import com.pt5.pthouduan.entity.User; |
| 5 | import com.pt5.pthouduan.service.UserService; |
| 6 | import org.junit.jupiter.api.BeforeEach; |
| 7 | import org.junit.jupiter.api.Test; |
| 8 | import org.mockito.InjectMocks; |
| 9 | import org.mockito.Mock; |
| 10 | import org.mockito.MockitoAnnotations; |
| 11 | import org.springframework.http.ResponseEntity; |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 12 | import org.springframework.mock.web.MockMultipartFile; |
| 13 | import org.springframework.web.multipart.MultipartFile; |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 14 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 15 | import java.io.IOException; |
| 16 | import java.nio.file.Path; |
| 17 | import java.nio.file.Paths; |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import static org.junit.jupiter.api.Assertions.*; |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.*; |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 23 | import static org.mockito.Mockito.*; |
| 24 | |
| 25 | class UserControllerTest { |
| 26 | |
| 27 | @Mock |
| 28 | private UserService userService; |
| 29 | |
| 30 | @InjectMocks |
| 31 | private UserController userController; |
| 32 | |
| 33 | @BeforeEach |
| 34 | void setUp() { |
| 35 | MockitoAnnotations.openMocks(this); |
| 36 | } |
| 37 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 38 | @Test |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 39 | void register_Success() { |
| 40 | // 准备测试数据 |
| 41 | User user = new User(0L,""); |
| 42 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 43 | expectedResponse.put("success", true); |
| 44 | expectedResponse.put("message", "注册成功"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 45 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 46 | // 模拟服务层行为 |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 47 | when(userService.register(any(User.class), anyString(), anyString())) |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 48 | .thenReturn(expectedResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 49 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 50 | // 执行测试 |
| 51 | Map<String, Object> actualResponse = userController.register(user, "invite123", "email123"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 52 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 53 | // 验证结果 |
| 54 | assertEquals(expectedResponse, actualResponse); |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 55 | verify(userService, times(1)).register(any(), anyString(), anyString()); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 58 | @Test |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 59 | void login_Success() { |
| 60 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 61 | expectedResponse.put("success", true); |
| 62 | expectedResponse.put("token", "test-token"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 63 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 64 | when(userService.login(anyString(), anyString())) |
| 65 | .thenReturn(expectedResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 66 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 67 | Map<String, Object> actualResponse = userController.login("testuser", "password123"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 68 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 69 | assertEquals(expectedResponse, actualResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 70 | } |
| 71 | |
21301050 | f5f827d | 2025-06-09 15:09:33 +0800 | [diff] [blame^] | 72 | // @Test |
| 73 | // void uploadAvatar_Success() throws IOException { |
| 74 | // // 准备测试文件 |
| 75 | // MultipartFile file = new MockMultipartFile( |
| 76 | // "avatar", |
| 77 | // "test.jpg", |
| 78 | // "image/jpeg", |
| 79 | // "test image content".getBytes() |
| 80 | // ); |
| 81 | // |
| 82 | // // 模拟服务层行为 |
| 83 | // //doNothing().when(userService).changeImage(anyString(), anyString()); |
| 84 | // |
| 85 | // // 执行测试 |
| 86 | // ResponseEntity<Map<String, Object>> response = userController.uploadAvatar(file); |
| 87 | // |
| 88 | // // 验证结果 |
| 89 | // assertEquals(200, response.getStatusCodeValue()); |
| 90 | // assertTrue((Boolean) response.getBody().get("success")); |
| 91 | // assertNotNull(response.getBody().get("url")); |
| 92 | // |
| 93 | // // 验证文件是否保存到正确路径 |
| 94 | //// Path expectedPath = Paths.get("../var/www/avatars/").resolve(anyString()); |
| 95 | // // 实际项目中需要添加文件存在性断言 |
| 96 | // } |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 97 | |
21301050 | f5f827d | 2025-06-09 15:09:33 +0800 | [diff] [blame^] | 98 | // @Test |
| 99 | // void uploadAvatar_InvalidFileType() { |
| 100 | // MultipartFile file = new MockMultipartFile( |
| 101 | // "avatar", |
| 102 | // "test.txt", |
| 103 | // "text/plain", |
| 104 | // "invalid content".getBytes() |
| 105 | // ); |
| 106 | // |
| 107 | // ResponseEntity<Map<String, Object>> response = userController.uploadAvatar(file); |
| 108 | // |
| 109 | // assertEquals(400, response.getStatusCodeValue()); |
| 110 | // assertEquals("只支持JPG/PNG/GIF格式的图片", response.getBody().get("message")); |
| 111 | // } |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 112 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 113 | @Test |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 114 | void changeImage_Success() { |
| 115 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 116 | expectedResponse.put("success", true); |
| 117 | expectedResponse.put("message", "头像更改成功"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 118 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 119 | // 执行测试 |
| 120 | Map<String, Object> actualResponse = userController.changeimage("testuser", "new-avatar.jpg"); |
| 121 | |
| 122 | // 验证结果 |
| 123 | assertEquals(expectedResponse, actualResponse); |
| 124 | verify(userService, times(1)).changeImage("testuser", "new-avatar.jpg"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 127 | @Test |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 128 | void changePassword_Success() { |
| 129 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 130 | expectedResponse.put("success", true); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 131 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 132 | when(userService.changePassword(anyString(), anyString(), anyString())) |
| 133 | .thenReturn(expectedResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 134 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 135 | Map<String, Object> actualResponse = userController.changePassword( |
| 136 | "testuser", "oldPass", "newPass"); |
| 137 | |
| 138 | assertEquals(expectedResponse, actualResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 139 | } |
| 140 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 141 | // 其他方法测试示例 |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 142 | @Test |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 143 | void getUserInfo_Success() { |
| 144 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 145 | expectedResponse.put("username", "testuser"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 146 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 147 | when(userService.UserInfo(anyString())) |
| 148 | .thenReturn(expectedResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 149 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 150 | Map<String, Object> actualResponse = userController.getuser("testuser"); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 151 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 152 | assertEquals(expectedResponse, actualResponse); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 153 | } |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 154 | } |