夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame^] | 1 | package com.example.g8backend.service; |
| 2 | |
| 3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 4 | import com.example.g8backend.entity.Follow; |
| 5 | import com.example.g8backend.entity.Message; |
| 6 | import com.example.g8backend.entity.User; |
| 7 | import com.example.g8backend.mapper.FollowMapper; |
| 8 | import com.example.g8backend.mapper.MessageMapper; |
| 9 | import com.example.g8backend.mapper.UserMapper; |
| 10 | import com.example.g8backend.service.impl.UserServiceImpl; |
| 11 | import org.junit.jupiter.api.BeforeEach; |
| 12 | import org.junit.jupiter.api.Test; |
| 13 | import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | import org.mockito.InjectMocks; |
| 15 | import org.mockito.Mock; |
| 16 | import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | import org.springframework.test.util.ReflectionTestUtils; |
| 18 | |
| 19 | import java.time.LocalDateTime; |
| 20 | import java.util.Arrays; |
| 21 | import java.util.Collections; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import static org.junit.jupiter.api.Assertions.*; |
| 25 | import static org.mockito.ArgumentMatchers.any; |
| 26 | import static org.mockito.ArgumentMatchers.anyLong; |
| 27 | import static org.mockito.Mockito.*; |
| 28 | |
| 29 | import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | |
| 31 | @ExtendWith(MockitoExtension.class) |
| 32 | public class UserServiceImplTest { |
| 33 | |
| 34 | @Mock |
| 35 | private UserMapper userMapper; |
| 36 | |
| 37 | @Mock |
| 38 | private FollowMapper followMapper; |
| 39 | |
| 40 | @Mock |
| 41 | private MessageMapper messageMapper; |
| 42 | |
| 43 | @InjectMocks |
| 44 | private UserServiceImpl userService; |
| 45 | |
| 46 | private User user1; |
| 47 | private User user2; |
| 48 | private Follow follow; |
| 49 | private Message message; |
| 50 | |
| 51 | @BeforeEach |
| 52 | void setUp() { |
| 53 | user1 = new User().setUserId(1L).setUserName("user1"); |
| 54 | user2 = new User().setUserId(2L).setUserName("user2"); |
| 55 | ReflectionTestUtils.setField(userService, "baseMapper", userMapper); |
| 56 | |
| 57 | follow = new Follow().setFollowerId(1L).setFollowedId(2L); |
| 58 | message = new Message() |
| 59 | .setMessageId(100L) |
| 60 | .setSenderId(1L) |
| 61 | .setReceiverId(2L) |
| 62 | .setContent("Hello") |
| 63 | .setIsRead(false) |
| 64 | .setSentAt(LocalDateTime.now()); |
| 65 | } |
| 66 | |
| 67 | // ------------------------- 关注功能测试 ------------------------- |
| 68 | @Test |
| 69 | void followUser_Success() { |
| 70 | // 模拟Mapper行为 |
| 71 | when(followMapper.insert(any(Follow.class))).thenReturn(1); |
| 72 | |
| 73 | // 测试关注 |
| 74 | boolean result = userService.followUser(1L, 2L); |
| 75 | assertTrue(result); |
| 76 | verify(followMapper, times(1)).insert(any(Follow.class)); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | void followUser_SelfFollow_Fail() { |
| 81 | // 尝试关注自己 |
| 82 | boolean result = userService.followUser(1L, 1L); |
| 83 | assertFalse(result); |
| 84 | verify(followMapper, never()).insert((Follow) any()); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | void unfollowUser_Success() { |
| 89 | // 模拟Mapper行为 |
| 90 | when(followMapper.deleteByPair(1L, 2L)).thenReturn(1); |
| 91 | |
| 92 | // 测试取消关注 |
| 93 | boolean result = userService.unfollowUser(1L, 2L); |
| 94 | assertTrue(result); |
| 95 | verify(followMapper, times(1)).deleteByPair(1L, 2L); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | @Test |
| 100 | void getFollowers_EmptyList() { |
| 101 | // 模拟无粉丝 |
| 102 | when(followMapper.selectFollowers(1L)).thenReturn(Collections.emptyList()); |
| 103 | |
| 104 | // 测试获取粉丝列表 |
| 105 | List<User> followers = userService.getFollowers(1L); |
| 106 | assertTrue(followers.isEmpty()); |
| 107 | } |
| 108 | |
| 109 | // ------------------------- 私信功能测试 ------------------------- |
| 110 | @Test |
| 111 | void sendMessage_Success() { |
| 112 | // 模拟用户存在 |
| 113 | when(userMapper.selectById(2L)).thenReturn(user2); |
| 114 | when(messageMapper.insert(any(Message.class))).thenAnswer(invocation -> { |
| 115 | Message msg = invocation.getArgument(0); |
| 116 | msg.setMessageId(100L); |
| 117 | return 1; |
| 118 | }); |
| 119 | |
| 120 | // 测试发送消息 |
| 121 | Long messageId = userService.sendMessage(1L, 2L, "Hello"); |
| 122 | assertNotNull(messageId); |
| 123 | verify(messageMapper, times(1)).insert(any(Message.class)); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | void sendMessage_ReceiverNotExist_ThrowException() { |
| 128 | // 模拟用户不存在 |
| 129 | when(userMapper.selectById(99L)).thenReturn(null); |
| 130 | |
| 131 | // 测试异常 |
| 132 | assertThrows(RuntimeException.class, () -> |
| 133 | userService.sendMessage(1L, 99L, "Hello") |
| 134 | ); |
| 135 | verify(messageMapper, never()).insert((Message) any()); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | void getMessageHistory_SortedByTime() { |
| 140 | // 模拟历史消息 |
| 141 | Message oldMessage = message.setSentAt(LocalDateTime.now().minusHours(1)); |
| 142 | when(messageMapper.selectUserMessages(1L)) |
| 143 | .thenReturn(Arrays.asList(oldMessage, message)); |
| 144 | |
| 145 | // 测试按时间倒序排序 |
| 146 | List<Message> history = userService.getMessageHistory(1L); |
| 147 | assertEquals(message.getMessageId(), history.get(0).getMessageId()); |
| 148 | } |
| 149 | } |