夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.service; |
| 2 | |
| 3 | import com.example.g8backend.entity.PostRating; |
| 4 | import com.example.g8backend.mapper.PostMapper; |
| 5 | import com.example.g8backend.mapper.PostRatingMapper; |
| 6 | import com.example.g8backend.service.impl.PostRatingServiceImpl; |
| 7 | import org.junit.jupiter.api.BeforeEach; |
| 8 | import org.junit.jupiter.api.Test; |
| 9 | import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | import org.mockito.InjectMocks; |
| 11 | import org.mockito.Mock; |
| 12 | import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | import org.springframework.transaction.annotation.Transactional; |
| 14 | |
| 15 | import static org.junit.jupiter.api.Assertions.*; |
| 16 | import static org.mockito.ArgumentMatchers.*; |
| 17 | import static org.mockito.Mockito.*; |
| 18 | |
| 19 | @ExtendWith(MockitoExtension.class) |
| 20 | @Transactional |
| 21 | public class PostRatingServiceImplTest { |
| 22 | |
| 23 | @Mock |
| 24 | private PostRatingMapper postRatingMapper; |
| 25 | |
| 26 | @Mock |
| 27 | private PostMapper postMapper; |
| 28 | |
| 29 | @InjectMocks |
| 30 | private PostRatingServiceImpl postRatingService; |
| 31 | |
| 32 | private final Long userId = 1L; |
| 33 | private final Long postId = 100L; |
| 34 | private final Integer validRating = 4; |
| 35 | private final Integer invalidRating = 6; |
| 36 | |
| 37 | // 测试:合法评分应成功 |
| 38 | @Test |
| 39 | public void testRatePost_Success() { |
| 40 | // 模拟依赖行为 |
| 41 | when(postRatingMapper.insertOrUpdate(any(PostRating.class))).thenReturn(true); |
| 42 | when(postRatingMapper.calculateAverageRating(postId)).thenReturn(4.0); |
| 43 | when(postRatingMapper.getRatingCount(postId)).thenReturn(1); |
| 44 | |
| 45 | // 调用方法并验证无异常 |
| 46 | assertDoesNotThrow(() -> { |
| 47 | postRatingService.ratePost(userId, postId, validRating); |
| 48 | }); |
| 49 | |
| 50 | // 验证数据库交互 |
| 51 | verify(postMapper).updateRatingStats(eq(postId), eq(4.0), eq(1)); |
| 52 | } |
| 53 | |
| 54 | // 测试:非法评分应抛出异常 |
| 55 | @Test |
| 56 | public void testRatePost_InvalidRating() { |
| 57 | // 调用方法并验证异常 |
| 58 | IllegalArgumentException exception = assertThrows( |
| 59 | IllegalArgumentException.class, |
| 60 | () -> postRatingService.ratePost(userId, postId, invalidRating) |
| 61 | ); |
| 62 | assertEquals("评分值必须在1到5之间", exception.getMessage()); |
| 63 | |
| 64 | // 验证未调用数据库操作 |
| 65 | verifyNoInteractions(postRatingMapper); |
| 66 | } |
| 67 | |
| 68 | // 测试:重复评分应更新记录 |
| 69 | @Test |
| 70 | public void testRatePost_UpdateExistingRating() { |
| 71 | // 模拟已存在评分 |
| 72 | when(postRatingMapper.insertOrUpdate(any(PostRating.class))).thenReturn(true); |
| 73 | when(postRatingMapper.calculateAverageRating(postId)).thenReturn(3.5, 4.0); // 两次调用返回不同值 |
| 74 | when(postRatingMapper.getRatingCount(postId)).thenReturn(1); |
| 75 | |
| 76 | // 同一用户对同一帖子二次评分 |
| 77 | assertDoesNotThrow(() -> { |
| 78 | postRatingService.ratePost(userId, postId, 3); |
| 79 | postRatingService.ratePost(userId, postId, 4); |
| 80 | }); |
| 81 | |
| 82 | // 验证两次更新统计信息 |
| 83 | verify(postMapper, times(2)).updateRatingStats(eq(postId), anyDouble(), eq(1)); |
| 84 | } |
| 85 | |
| 86 | // 测试:数据库操作失败应抛出异常 |
| 87 | @Test |
| 88 | public void testRatePost_DatabaseFailure() { |
| 89 | when(postRatingMapper.insertOrUpdate(any(PostRating.class))).thenReturn(false); |
| 90 | RuntimeException exception = assertThrows( |
| 91 | RuntimeException.class, |
| 92 | () -> postRatingService.ratePost(userId, postId, validRating) |
| 93 | ); |
| 94 | assertEquals("评分操作失败", exception.getMessage()); |
| 95 | } |
| 96 | // 测试:获取评分用户数量 |
| 97 | @Test |
| 98 | public void testGetRatingUserCount() { |
| 99 | when(postRatingMapper.selectRatingUserCount(postId)).thenReturn(5L); |
| 100 | Long count = postRatingService.getRatingUserCount(postId); |
| 101 | assertEquals(5L, count); |
| 102 | } |
| 103 | } |