xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame^] | 1 | package com.g9.g9backend.controller; |
| 2 | |
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | import com.g9.g9backend.pojo.*; |
| 5 | import com.g9.g9backend.pojo.DTO.*; |
| 6 | import com.g9.g9backend.service.*; |
| 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 static org.mockito.ArgumentMatchers.*; |
| 17 | import static org.mockito.Mockito.when; |
| 18 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 19 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 20 | |
| 21 | public class UserControllerTest { |
| 22 | |
| 23 | private MockMvc mockMvc; |
| 24 | |
| 25 | @InjectMocks |
| 26 | private UserController userController; |
| 27 | |
| 28 | @Mock |
| 29 | private UserService userService; |
| 30 | |
| 31 | @Mock |
| 32 | private InvitationService invitationService; |
| 33 | |
| 34 | @Mock |
| 35 | private SubscriptionService subscriptionService; |
| 36 | |
| 37 | private final ObjectMapper objectMapper = new ObjectMapper(); |
| 38 | |
| 39 | @BeforeEach |
| 40 | public void setup() { |
| 41 | MockitoAnnotations.openMocks(this); |
| 42 | mockMvc = MockMvcBuilders.standaloneSetup(userController).build(); |
| 43 | } |
| 44 | |
| 45 | // 注册 |
| 46 | @Test |
| 47 | public void testRegister_success() throws Exception { |
| 48 | /* |
| 49 | 注意单元测试中(使用Mockito ),当多次为同一个方法设置不同的返回值时,后面的设置会覆盖前面的设置 |
| 50 | 可见单元测试在执行时,本质上在跑实际接口实现,当遇到被mock模拟的方法时,会来测试代码里找对应的when...thenReturn,直接看最后一个设置,所以当测试方法时,测试代码里写的和实际接口的顺序上不是一一对应 |
| 51 | 而是测试代码里写B C A,实际接口代码里写A B C,当实际代码跑到A时去测试代码里从后往前找到A,而不是看测试代码里第一个是不是A若是再用的逻辑 |
| 52 | 所以如果写成如下形式: |
| 53 | when(userService.getOne(any())).thenReturn(null); |
| 54 | ....其他代码 |
| 55 | when(userService.getOne(any())).thenReturn(new User(2, "hcy", "123", null, 0, 0, null, 0, 0, 0)); |
| 56 | 则下面的设置会覆盖上面的设置,导致无论什么时候调用该方法,返回的都是非空对象 |
| 57 | 所以应该写成如下形式: |
| 58 | when(userService.getOne(any())) |
| 59 | .thenReturn(null) // 第一次调用返回null |
| 60 | .thenReturn(new User(2, "hcy", "123", null, 0, 0, null, 0, 0, 0)); // 第二次调用返回非空对象 |
| 61 | */ |
| 62 | |
| 63 | when(userService.getOne(any())) |
| 64 | .thenReturn(null) // 第一次调用返回null,表示用户名不是已存在的 |
| 65 | .thenReturn(new User(2, "hcy", "123", null, 0, 0, null, 0, 0, 0)); // 第二次调用返回非空对象,表示获取添加后的用户信息 |
| 66 | // 邀请码存在且未被使用 |
| 67 | when(invitationService.getOne(any())).thenReturn(new Invitation("6RCA7Y8J", 1, 0)); |
| 68 | // 添加新用户 |
| 69 | when(userService.save(any())).thenReturn(true); |
| 70 | // 设置该邀请码被新添加的新用户使用 |
| 71 | when(invitationService.update(any())).thenReturn(true); |
| 72 | // 获取五个新邀请码 |
| 73 | when(invitationService.generateInvitationCode()).thenReturn(new String[5]); |
| 74 | // 分配给新用户邀请码 |
| 75 | when(invitationService.save(any())).thenReturn(true); |
| 76 | mockMvc.perform(post("/user/register") |
| 77 | .contentType(MediaType.APPLICATION_JSON) |
| 78 | .content(objectMapper.writeValueAsString(new RegisterDTO("hcy", "123", "6RCA7Y8J")))) |
| 79 | .andExpect(status().isOk()); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testRegister_userExists() throws Exception { |
| 84 | // 用户名不是已存在的 |
| 85 | when(userService.getOne(any())).thenReturn(new User(2, "hcy", "123", null, 0, 0, null, 0, 0, 0)); |
| 86 | mockMvc.perform(post("/user/register") |
| 87 | .contentType(MediaType.APPLICATION_JSON) |
| 88 | .content(objectMapper.writeValueAsString(new RegisterDTO("hcy", "123", "6RCA7Y8J")))) |
| 89 | .andExpect(status().is(407)); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testRegister_invitationNotExist() throws Exception { |
| 94 | // 用户名不是已存在的 |
| 95 | when(userService.getOne(any())).thenReturn(null); |
| 96 | // 邀请码不存在 |
| 97 | when(invitationService.getOne(any())).thenReturn(null); |
| 98 | mockMvc.perform(post("/user/register") |
| 99 | .contentType(MediaType.APPLICATION_JSON) |
| 100 | .content(objectMapper.writeValueAsString(new RegisterDTO("hcy", "123", "6RCA7Y8J")))) |
| 101 | .andExpect(status().is(409)); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testRegister_invitationUsed() throws Exception { |
| 106 | // 用户名不是已存在的 |
| 107 | when(userService.getOne(any())).thenReturn(null); |
| 108 | // 邀请码存在但已被使用 |
| 109 | when(invitationService.getOne(any())).thenReturn(new Invitation("6RCA7Y8J", 1, 2)); |
| 110 | mockMvc.perform(post("/user/register") |
| 111 | .contentType(MediaType.APPLICATION_JSON) |
| 112 | .content(objectMapper.writeValueAsString(new RegisterDTO("hcy", "123", "6RCA7Y8J")))) |
| 113 | .andExpect(status().is(410)); |
| 114 | } |
| 115 | |
| 116 | // 登录 |
| 117 | @Test |
| 118 | public void testLogin_success() throws Exception { |
| 119 | // 设置请求参数 |
| 120 | User user = new User(); |
| 121 | user.setUsername("hcy"); |
| 122 | user.setPassword("123"); |
| 123 | |
| 124 | when(userService.getOne(any())).thenReturn(new User(1, "hcy", "123", null, 0, 0, null, 0, 0, 0)); |
| 125 | |
| 126 | mockMvc.perform(post("/user/login") |
| 127 | .contentType(MediaType.APPLICATION_JSON) |
| 128 | .content(objectMapper.writeValueAsString(user))) |
| 129 | .andExpect(status().isOk()); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testLogin_userNotFound() throws Exception { |
| 134 | //设置请求参数 |
| 135 | User user = new User(); |
| 136 | user.setUsername("hcy"); |
| 137 | user.setPassword("123"); |
| 138 | |
| 139 | when(userService.getOne(any())).thenReturn(null); |
| 140 | |
| 141 | mockMvc.perform(post("/user/login") |
| 142 | .contentType(MediaType.APPLICATION_JSON) |
| 143 | .content(objectMapper.writeValueAsString(user))) |
| 144 | .andExpect(status().is(406)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testLogin_wrongPassword() throws Exception { |
| 149 | // 设置请求参数 |
| 150 | User user = new User(); |
| 151 | user.setUsername("hcy"); |
| 152 | user.setPassword("wrongPassword"); |
| 153 | |
| 154 | when(userService.getOne(any())).thenReturn(new User(1, "hcy", "123", null, 0, 0, null, 0, 0, 0)); |
| 155 | |
| 156 | mockMvc.perform(post("/user/login") |
| 157 | .contentType(MediaType.APPLICATION_JSON) |
| 158 | .content(objectMapper.writeValueAsString(user))) |
| 159 | .andExpect(status().is(408)); |
| 160 | } |
| 161 | |
| 162 | // 关注 |
| 163 | @Test |
| 164 | public void testSubscription() throws Exception { |
| 165 | when(subscriptionService.save(any())).thenReturn(true); |
| 166 | |
| 167 | mockMvc.perform(post("/user/subscription") |
| 168 | .contentType(MediaType.APPLICATION_JSON) |
| 169 | .content(objectMapper.writeValueAsString(new Subscription(1, 2)))) |
| 170 | .andExpect(status().isOk()); |
| 171 | } |
| 172 | } |