ym923 | 55ec83a | 2025-06-03 17:15:13 +0800 | [diff] [blame] | 1 | package com.pt5.pthouduan.service; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.Comment; |
| 4 | import org.junit.jupiter.api.BeforeEach; |
| 5 | import org.junit.jupiter.api.Test; |
| 6 | import org.mockito.Mock; |
| 7 | import org.mockito.MockitoAnnotations; |
| 8 | |
| 9 | import java.time.LocalDateTime; |
| 10 | import java.util.Collections; |
| 11 | import java.util.List; |
| 12 | |
| 13 | import static org.junit.jupiter.api.Assertions.*; |
| 14 | import static org.mockito.Mockito.*; |
| 15 | |
| 16 | class CommentServiceTest { |
| 17 | |
| 18 | @Mock |
| 19 | private CommentService commentService; |
| 20 | |
| 21 | @BeforeEach |
| 22 | void setUp() { |
| 23 | MockitoAnnotations.openMocks(this); |
| 24 | } |
| 25 | |
| 26 | @Test |
| 27 | void createComment_ShouldReturnCreatedComment() { |
| 28 | Comment newComment = new Comment(); |
| 29 | newComment.setUserid(1L); |
| 30 | newComment.setPostid(10); |
| 31 | newComment.setPostCommentcontent("这是一个评论"); |
| 32 | newComment.setCommenttime(LocalDateTime.now()); |
| 33 | |
| 34 | when(commentService.createComment(any(Comment.class))).thenReturn(newComment); |
| 35 | |
| 36 | Comment result = commentService.createComment(newComment); |
| 37 | |
| 38 | assertNotNull(result); |
| 39 | assertEquals("这是一个评论", result.getPostCommentcontent()); |
| 40 | verify(commentService).createComment(newComment); |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | void deleteComment_ShouldReturnTrue() { |
| 45 | when(commentService.deleteComment(1)).thenReturn(true); |
| 46 | |
| 47 | boolean result = commentService.deleteComment(1); |
| 48 | |
| 49 | assertTrue(result); |
| 50 | verify(commentService).deleteComment(1); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | void updateComment_ShouldReturnTrue() { |
| 55 | Comment comment = new Comment(); |
| 56 | comment.setCommentid(1); |
| 57 | comment.setPostCommentcontent("更新的评论"); |
| 58 | |
| 59 | when(commentService.updateComment(comment)).thenReturn(true); |
| 60 | |
| 61 | boolean result = commentService.updateComment(comment); |
| 62 | |
| 63 | assertTrue(result); |
| 64 | verify(commentService).updateComment(comment); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | void getCommentsByPostId_ShouldReturnCommentsList() { |
| 69 | Comment comment = new Comment(); |
| 70 | comment.setPostid(10); |
| 71 | comment.setPostCommentcontent("测试评论"); |
| 72 | |
| 73 | when(commentService.getCommentsByPostId(10)).thenReturn(Collections.singletonList(comment)); |
| 74 | |
| 75 | List<Comment> result = commentService.getCommentsByPostId(10); |
| 76 | |
| 77 | assertEquals(1, result.size()); |
| 78 | assertEquals("测试评论", result.get(0).getPostCommentcontent()); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | void likeComment_ShouldReturnTrue() { |
| 83 | when(commentService.likeComment(1)).thenReturn(true); |
| 84 | |
| 85 | boolean result = commentService.likeComment(1); |
| 86 | |
| 87 | assertTrue(result); |
| 88 | verify(commentService).likeComment(1); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | void unlikeComment_ShouldReturnTrue() { |
| 93 | when(commentService.unlikeComment(1)).thenReturn(true); |
| 94 | |
| 95 | boolean result = commentService.unlikeComment(1); |
| 96 | |
| 97 | assertTrue(result); |
| 98 | verify(commentService).unlikeComment(1); |
| 99 | } |
| 100 | } |