blob: 18707e24fd7dfd80dbc625270ca520878e0d2223 [file] [log] [blame]
Sure2338188c5d2025-05-28 11:43:06 +08001package com.pt5.pthouduan.ControllerTest;
Sure2338188c5d2025-05-28 11:43:06 +08002import com.pt5.pthouduan.controller.UserController;
3import com.pt5.pthouduan.entity.User;
4import com.pt5.pthouduan.service.UserService;
5import org.junit.jupiter.api.BeforeEach;
6import org.junit.jupiter.api.Test;
7import org.mockito.InjectMocks;
8import org.mockito.Mock;
9import org.mockito.MockitoAnnotations;
10import org.springframework.http.ResponseEntity;
11
12import java.util.HashMap;
13import java.util.Map;
14
15import static org.junit.jupiter.api.Assertions.*;
21301050c519f712025-06-04 17:03:04 +080016import static org.mockito.ArgumentMatchers.any;
17import static org.mockito.ArgumentMatchers.anyString;
Sure2338188c5d2025-05-28 11:43:06 +080018import static org.mockito.Mockito.*;
19
20class UserControllerTest {
21
22 @Mock
23 private UserService userService;
24
25 @InjectMocks
26 private UserController userController;
27
28 @BeforeEach
29 void setUp() {
30 MockitoAnnotations.openMocks(this);
31 }
32
21301050c519f712025-06-04 17:03:04 +080033 // 测试注册功能
Sure2338188c5d2025-05-28 11:43:06 +080034 @Test
21301050c519f712025-06-04 17:03:04 +080035 void register_ShouldReturnSuccess() {
36 User user = new User(0L,"1");
37 user.setUsername("testUser");
38 Map<String, Object> expected = new HashMap<>();
39 expected.put("status", "success");
Sure2338188c5d2025-05-28 11:43:06 +080040
21301050c519f712025-06-04 17:03:04 +080041 when(userService.register(any(User.class), anyString(), anyString()))
42 .thenReturn(expected);
Sure2338188c5d2025-05-28 11:43:06 +080043
21301050c519f712025-06-04 17:03:04 +080044 Map<String, Object> result = userController.register(user, "INVITE123", "EMAIL456");
Sure2338188c5d2025-05-28 11:43:06 +080045
21301050c519f712025-06-04 17:03:04 +080046 assertEquals(expected, result);
47 verify(userService, times(1)).register(any(), anyString(), anyString());
Sure2338188c5d2025-05-28 11:43:06 +080048 }
49
21301050c519f712025-06-04 17:03:04 +080050 // 测试登录功能
Sure2338188c5d2025-05-28 11:43:06 +080051 @Test
21301050c519f712025-06-04 17:03:04 +080052 void login_ShouldReturnToken() {
53 Map<String, Object> expected = new HashMap<>();
54 expected.put("token", "mockToken123");
Sure2338188c5d2025-05-28 11:43:06 +080055
21301050c519f712025-06-04 17:03:04 +080056 when(userService.login("testUser", "password123"))
57 .thenReturn(expected);
Sure2338188c5d2025-05-28 11:43:06 +080058
21301050c519f712025-06-04 17:03:04 +080059 Map<String, Object> result = userController.login("testUser", "password123");
Sure2338188c5d2025-05-28 11:43:06 +080060
21301050c519f712025-06-04 17:03:04 +080061 assertEquals(expected, result);
62 assertNotNull(result.get("token"));
Sure2338188c5d2025-05-28 11:43:06 +080063 }
64
21301050c519f712025-06-04 17:03:04 +080065 // 测试获取用户信息
Sure2338188c5d2025-05-28 11:43:06 +080066 @Test
21301050c519f712025-06-04 17:03:04 +080067 void getUserInfo_ShouldReturnUserData() {
68 Map<String, Object> expected = new HashMap<>();
69 expected.put("username", "testUser");
70 expected.put("email", "test@example.com");
Sure2338188c5d2025-05-28 11:43:06 +080071
21301050c519f712025-06-04 17:03:04 +080072 when(userService.UserInfo("testUser"))
73 .thenReturn(expected);
Sure2338188c5d2025-05-28 11:43:06 +080074
21301050c519f712025-06-04 17:03:04 +080075 Map<String, Object> result = userController.getuser("testUser");
Sure2338188c5d2025-05-28 11:43:06 +080076
21301050c519f712025-06-04 17:03:04 +080077 assertEquals("testUser", result.get("username"));
78 assertEquals("test@example.com", result.get("email"));
Sure2338188c5d2025-05-28 11:43:06 +080079 }
80
21301050c519f712025-06-04 17:03:04 +080081 // 测试修改密码
Sure2338188c5d2025-05-28 11:43:06 +080082 @Test
21301050c519f712025-06-04 17:03:04 +080083 void changePassword_ShouldHandleSuccess() {
84 Map<String, Object> expected = new HashMap<>();
85 expected.put("message", "Password updated");
Sure2338188c5d2025-05-28 11:43:06 +080086
21301050c519f712025-06-04 17:03:04 +080087 when(userService.changePassword("testUser", "oldPass", "newPass"))
88 .thenReturn(expected);
Sure2338188c5d2025-05-28 11:43:06 +080089
21301050c519f712025-06-04 17:03:04 +080090 Map<String, Object> result = userController.changePassword(
91 "testUser", "oldPass", "newPass");
Sure2338188c5d2025-05-28 11:43:06 +080092
21301050c519f712025-06-04 17:03:04 +080093 assertEquals("Password updated", result.get("message"));
Sure2338188c5d2025-05-28 11:43:06 +080094 }
95
21301050c519f712025-06-04 17:03:04 +080096 // 测试异常情况
Sure2338188c5d2025-05-28 11:43:06 +080097 @Test
21301050c519f712025-06-04 17:03:04 +080098 void login_ShouldHandleFailure() {
99 when(userService.login("wrongUser", "wrongPass"))
100 .thenThrow(new RuntimeException("Invalid credentials"));
Sure2338188c5d2025-05-28 11:43:06 +0800101
21301050c519f712025-06-04 17:03:04 +0800102 assertThrows(RuntimeException.class, () -> {
103 userController.login("wrongUser", "wrongPass");
104 });
Sure2338188c5d2025-05-28 11:43:06 +0800105 }
106
21301050c519f712025-06-04 17:03:04 +0800107 // 测试空参数情况
Sure2338188c5d2025-05-28 11:43:06 +0800108 @Test
21301050c519f712025-06-04 17:03:04 +0800109 void getUserInfo_ShouldHandleNullUsername() {
110 when(userService.UserInfo(null))
111 .thenReturn(Map.of("error", "Username required"));
Sure2338188c5d2025-05-28 11:43:06 +0800112
21301050c519f712025-06-04 17:03:04 +0800113 Map<String, Object> result = userController.getuser(null);
Sure2338188c5d2025-05-28 11:43:06 +0800114
21301050c519f712025-06-04 17:03:04 +0800115 assertEquals("Username required", result.get("error"));
Sure2338188c5d2025-05-28 11:43:06 +0800116 }
117
21301050c519f712025-06-04 17:03:04 +0800118 // 测试创建用户
Sure2338188c5d2025-05-28 11:43:06 +0800119 @Test
21301050c519f712025-06-04 17:03:04 +0800120 void createUser_ShouldReturnUserId() {
121 User newUser = new User(0L,"1");
122 newUser.setUsername("newUser");
123 Map<String, Object> expected = Map.of("userId", 123);
Sure2338188c5d2025-05-28 11:43:06 +0800124
21301050c519f712025-06-04 17:03:04 +0800125 when(userService.CreateUser(any(User.class)))
126 .thenReturn(expected);
Sure2338188c5d2025-05-28 11:43:06 +0800127
21301050c519f712025-06-04 17:03:04 +0800128 Map<String, Object> result = userController.creatUser(newUser);
Sure2338188c5d2025-05-28 11:43:06 +0800129
21301050c519f712025-06-04 17:03:04 +0800130 assertEquals(123, result.get("userId"));
Sure2338188c5d2025-05-28 11:43:06 +0800131 }
132}