ym923 | 55ec83a | 2025-06-03 17:15:13 +0800 | [diff] [blame] | 1 | package com.pt5.pthouduan.controller; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.Comment; |
| 4 | import com.pt5.pthouduan.service.CommentService; |
| 5 | import org.junit.jupiter.api.BeforeEach; |
| 6 | import org.junit.jupiter.api.Test; |
| 7 | import org.mockito.InjectMocks; |
| 8 | import org.mockito.Mock; |
| 9 | import org.mockito.MockitoAnnotations; |
| 10 | import org.springframework.http.MediaType; |
| 11 | import org.springframework.test.web.servlet.MockMvc; |
| 12 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 13 | |
| 14 | import java.time.LocalDateTime; |
| 15 | import java.util.Arrays; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import static org.mockito.ArgumentMatchers.any; |
| 19 | import static org.mockito.Mockito.*; |
| 20 | import static org.junit.jupiter.api.Assertions.*; |
| 21 | |
| 22 | class CommentControllerTest { |
| 23 | |
| 24 | @Mock |
| 25 | private CommentService commentService; |
| 26 | |
| 27 | @InjectMocks |
| 28 | private CommentController commentController; |
| 29 | |
| 30 | private MockMvc mockMvc; |
| 31 | |
| 32 | @BeforeEach |
| 33 | void setUp() { |
| 34 | MockitoAnnotations.openMocks(this); |
| 35 | mockMvc = MockMvcBuilders.standaloneSetup(commentController).build(); |
| 36 | } |
| 37 | |
| 38 | @Test |
| 39 | void createComment_ShouldReturnCreatedComment() { |
| 40 | Comment inputComment = new Comment(); |
| 41 | inputComment.setPostid(1); |
| 42 | inputComment.setUserid(100L); |
| 43 | inputComment.setPostCommentcontent("Test Comment"); |
| 44 | inputComment.setCommenttime(LocalDateTime.now()); |
| 45 | |
| 46 | Comment savedComment = new Comment(); |
| 47 | savedComment.setCommentid(1); |
| 48 | savedComment.setPostid(1); |
| 49 | savedComment.setUserid(100L); |
| 50 | savedComment.setPostCommentcontent("Test Comment"); |
| 51 | savedComment.setCommenttime(inputComment.getCommenttime()); |
| 52 | |
| 53 | when(commentService.createComment(any(Comment.class))).thenReturn(savedComment); |
| 54 | |
| 55 | Comment result = commentController.createComment(inputComment); |
| 56 | |
| 57 | assertNotNull(result); |
| 58 | assertEquals("Test Comment", result.getPostCommentcontent()); |
| 59 | assertEquals(100L, result.getUserid()); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | void deleteComment_ShouldReturnTrue_WhenSuccess() { |
| 64 | when(commentService.deleteComment(1)).thenReturn(true); |
| 65 | |
| 66 | boolean result = commentController.deleteComment(1); |
| 67 | |
| 68 | assertTrue(result); |
| 69 | verify(commentService).deleteComment(1); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | void updateComment_ShouldReturnTrue_WhenSuccess() { |
| 74 | Comment updatedComment = new Comment(); |
| 75 | updatedComment.setCommentid(1); |
| 76 | updatedComment.setPostCommentcontent("Updated content"); |
| 77 | |
| 78 | when(commentService.updateComment(any(Comment.class))).thenReturn(true); |
| 79 | |
| 80 | boolean result = commentController.updateComment(updatedComment); |
| 81 | |
| 82 | assertTrue(result); |
| 83 | verify(commentService).updateComment(updatedComment); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | void getCommentsByPostId_ShouldReturnListOfComments() { |
| 88 | Comment comment1 = new Comment(); |
| 89 | comment1.setCommentid(1); |
| 90 | comment1.setPostid(10); |
| 91 | comment1.setPostCommentcontent("Comment 1"); |
| 92 | |
| 93 | Comment comment2 = new Comment(); |
| 94 | comment2.setCommentid(2); |
| 95 | comment2.setPostid(10); |
| 96 | comment2.setPostCommentcontent("Comment 2"); |
| 97 | |
| 98 | List<Comment> commentList = Arrays.asList(comment1, comment2); |
| 99 | |
| 100 | when(commentService.getCommentsByPostId(10)).thenReturn(commentList); |
| 101 | |
| 102 | List<Comment> result = commentController.getCommentsByPostId(10); |
| 103 | |
| 104 | assertEquals(2, result.size()); |
| 105 | assertEquals("Comment 1", result.get(0).getPostCommentcontent()); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | void likeComment_ShouldReturnTrue_WhenSuccess() { |
| 110 | when(commentService.likeComment(1)).thenReturn(true); |
| 111 | |
| 112 | boolean result = commentController.likeComment(1); |
| 113 | |
| 114 | assertTrue(result); |
| 115 | verify(commentService).likeComment(1); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | void unlikeComment_ShouldReturnTrue_WhenSuccess() { |
| 120 | when(commentService.unlikeComment(1)).thenReturn(true); |
| 121 | |
| 122 | boolean result = commentController.unlikeComment(1); |
| 123 | |
| 124 | assertTrue(result); |
| 125 | verify(commentService).unlikeComment(1); |
| 126 | } |
| 127 | } |