searchpost
Change-Id: I3b471ad217a958e2e8bf22c005b1a3fd36454151
diff --git a/src/test/java/com/example/g8backend/service/PostServiceTest.java b/src/test/java/com/example/g8backend/service/PostServiceTest.java
index 6f54177..0161229 100644
--- a/src/test/java/com/example/g8backend/service/PostServiceTest.java
+++ b/src/test/java/com/example/g8backend/service/PostServiceTest.java
@@ -149,4 +149,49 @@
assertTrue(result.isEmpty());
verify(postMapper).getPostsByUserId(999L);
}
+
+ // 新增测试方法:搜索帖子,支持多个标签
+ @Test
+ @DisplayName("搜索帖子-通过关键词和多个标签")
+ void searchPosts_WithKeywordAndTags_ShouldReturnPosts() {
+ // Arrange
+ List<Long> tagIds = Arrays.asList(1L, 2L); // 假设存在标签ID 1和2
+ String keyword = "测试内容";
+ String author = "作者";
+ List<Post> expectedPosts = Arrays.asList(testPost);
+
+ // 模拟PostMapper的searchPosts方法
+ when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
+
+ // Act
+ List<Post> result = postService.searchPosts(keyword, tagIds, author);
+
+ // Assert
+ assertNotNull(result);
+ assertFalse(result.isEmpty());
+ assertEquals(testPost.getPostId(), result.get(0).getPostId());
+ verify(postMapper).searchPosts(keyword, tagIds, author);
+ }
+
+ @Test
+ @DisplayName("搜索帖子-没有匹配的帖子")
+ void searchPosts_WhenNoPosts_ShouldReturnEmptyList() {
+ // Arrange
+ List<Long> tagIds = Arrays.asList(3L, 4L); // 假设标签ID 3和4没有匹配的帖子
+ String keyword = "不存在的内容";
+ String author = "不存在的作者";
+ List<Post> expectedPosts = Arrays.asList(); // 没有匹配的帖子
+
+ // 模拟PostMapper的searchPosts方法
+ when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
+
+ // Act
+ List<Post> result = postService.searchPosts(keyword, tagIds, author);
+
+ // Assert
+ assertNotNull(result);
+ assertTrue(result.isEmpty());
+ verify(postMapper).searchPosts(keyword, tagIds, author);
+ }
+
}
\ No newline at end of file