ym923 | 4b86221 | 2025-06-06 17:25:46 +0800 | [diff] [blame^] | 1 | package com.pt5.pthouduan.controller; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.Post; |
| 4 | import com.pt5.pthouduan.service.PostService; |
| 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.web.multipart.MultipartFile; |
| 11 | |
| 12 | import java.time.LocalDateTime; |
| 13 | import java.util.Arrays; |
| 14 | import java.util.Collections; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import static org.junit.jupiter.api.Assertions.*; |
| 18 | import static org.mockito.Mockito.*; |
| 19 | |
| 20 | class PostControllerTest { |
| 21 | |
| 22 | @Mock |
| 23 | private PostService postService; |
| 24 | |
| 25 | @InjectMocks |
| 26 | private PostController postController; |
| 27 | |
| 28 | @BeforeEach |
| 29 | void setUp() { |
| 30 | MockitoAnnotations.openMocks(this); |
| 31 | } |
| 32 | |
| 33 | @Test |
| 34 | void createPost_WithValidData_ShouldReturnTrue() { |
| 35 | MultipartFile photo = mock(MultipartFile.class); |
| 36 | when(photo.isEmpty()).thenReturn(true); // 模拟没有上传图片 |
| 37 | |
| 38 | Post mockPost = new Post(); |
| 39 | mockPost.setPostid(1); |
| 40 | when(postService.createPost(any(Post.class))).thenReturn(mockPost); |
| 41 | |
| 42 | boolean result = postController.createPost( |
| 43 | 1L, "标题", "内容", "标签", "全站", true, photo |
| 44 | ); |
| 45 | |
| 46 | assertTrue(result); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | void deletePost_ShouldReturnTrue_WhenServiceSucceeds() { |
| 51 | when(postService.deletePost(1)).thenReturn(true); |
| 52 | boolean result = postController.deletePost(1); |
| 53 | assertTrue(result); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | void updatePost_ShouldReturnTrue_WhenUpdateSucceeds() { |
| 58 | Post post = new Post(); |
| 59 | post.setPostid(1); |
| 60 | when(postService.updatePost(post)).thenReturn(true); |
| 61 | assertTrue(postController.updatePost(post)); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | void searchPosts_ShouldReturnMatchedPosts() { |
| 66 | Post post1 = new Post(); |
| 67 | post1.setPostTitle("关键字匹配"); |
| 68 | |
| 69 | when(postService.searchPostsByKeyword("关键字")).thenReturn(List.of(post1)); |
| 70 | |
| 71 | List<Post> result = postController.searchPosts("关键字"); |
| 72 | |
| 73 | assertEquals(1, result.size()); |
| 74 | assertEquals("关键字匹配", result.get(0).getPostTitle()); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | void likePost_ShouldReturnTrue() { |
| 79 | when(postService.incrementLikes(1)).thenReturn(true); |
| 80 | assertTrue(postController.likePost(1)); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | void unlikePost_ShouldReturnTrue() { |
| 85 | when(postService.decrementLikes(1)).thenReturn(true); |
| 86 | assertTrue(postController.unlikePost(1)); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | void pinPost_ShouldReturnTrue() { |
| 91 | when(postService.setPinnedStatus(1, true)).thenReturn(true); |
| 92 | assertTrue(postController.pinPost(1)); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | void unpinPost_ShouldReturnTrue() { |
| 97 | when(postService.setPinnedStatus(1, false)).thenReturn(true); |
| 98 | assertTrue(postController.unpinPost(1)); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | void findByUserid_ShouldReturnUserPosts() { |
| 103 | Post post = new Post(); |
| 104 | post.setUserid(1L); |
| 105 | when(postService.findByUserid(1L)).thenReturn(List.of(post)); |
| 106 | |
| 107 | List<Post> result = postController.findByUserid(1L); |
| 108 | assertEquals(1, result.size()); |
| 109 | assertEquals(1L, result.get(0).getUserid()); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | void findPinnedPosts_ShouldReturnPinnedList() { |
| 114 | Post post = new Post(); |
| 115 | post.setIsPinned(true); |
| 116 | when(postService.findPinnedPosts()).thenReturn(List.of(post)); |
| 117 | |
| 118 | List<Post> result = postController.findPinnedPosts(); |
| 119 | assertEquals(1, result.size()); |
| 120 | assertTrue(result.get(0).getIsPinned()); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | void togglePinStatus_ShouldToggleAndReturnNewStatus() { |
| 125 | Post post = new Post(); |
| 126 | post.setPostid(1); |
| 127 | post.setIsPinned(false); |
| 128 | |
| 129 | when(postService.getById(1)).thenReturn(post); |
| 130 | when(postService.setPinnedStatus(1, true)).thenReturn(true); |
| 131 | |
| 132 | Boolean result = postController.togglePinStatus(1); |
| 133 | assertTrue(result); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | void getAllPostsSorted_ShouldReturnAllPosts() { |
| 138 | Post post1 = new Post(); |
| 139 | Post post2 = new Post(); |
| 140 | when(postService.getAllPostsSorted()).thenReturn(Arrays.asList(post1, post2)); |
| 141 | |
| 142 | List<Post> posts = postController.getAllPostsSorted(); |
| 143 | assertEquals(2, posts.size()); |
| 144 | } |
| 145 | } |