bug_fix+historyview
Change-Id: I6f446c1b660a4322865cfcf5502c88cb772ca0a1
diff --git a/src/test/java/com/example/g8backend/service/PostHistoryServiceTest.java b/src/test/java/com/example/g8backend/service/PostHistoryServiceTest.java
new file mode 100644
index 0000000..143422d
--- /dev/null
+++ b/src/test/java/com/example/g8backend/service/PostHistoryServiceTest.java
@@ -0,0 +1,75 @@
+package com.example.g8backend.service;
+
+import com.example.g8backend.entity.PostView;
+import com.example.g8backend.mapper.PostMapper;
+import com.example.g8backend.mapper.PostViewMapper;
+import com.example.g8backend.service.impl.PostServiceImpl;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentMatcher;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.time.LocalDateTime;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.*;
+
+@ExtendWith(MockitoExtension.class)
+@Transactional
+public class PostHistoryServiceTest {
+
+ @Mock
+ private PostViewMapper postViewMapper;
+
+ @Mock
+ private PostMapper postMapper;
+
+ @InjectMocks
+ private PostServiceImpl postService;
+
+ @Test
+ public void testRecordViewHistory_NormalCase() {
+ // 测试数据
+ Long userId = 1L;
+ Long postId = 100L;
+
+ // 调用方法
+ postService.recordViewHistory(userId, postId);
+
+ // 验证行为
+ verify(postViewMapper, times(1)).insert(any(PostView.class));
+ verify(postMapper, times(1)).incrementViewCount(eq(postId));
+ }
+
+ @Test
+ public void testRecordViewHistory_CheckDataIntegrity() {
+ Long userId = 2L;
+ Long postId = 200L;
+
+ postService.recordViewHistory(userId, postId);
+
+ // 显式指定参数类型为 PostView
+ verify(postViewMapper).insert(argThat(new ArgumentMatcher<PostView>() {
+ @Override
+ public boolean matches(PostView view) {
+ return view.getUserId().equals(userId) &&
+ view.getPostId().equals(postId) &&
+ view.getViewTime() != null;
+ }
+ }));
+ }
+ @Test
+ public void testRecordViewHistory_MultipleCalls() {
+ // 模拟多次调用
+ Long postId = 300L;
+ postService.recordViewHistory(1L, postId);
+ postService.recordViewHistory(2L, postId);
+
+ // 验证浏览数更新次数
+ verify(postMapper, times(2)).incrementViewCount(postId);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/example/g8backend/service/PostServiceTest.java b/src/test/java/com/example/g8backend/service/PostServiceTest.java
index 0161229..9f8fb14 100644
--- a/src/test/java/com/example/g8backend/service/PostServiceTest.java
+++ b/src/test/java/com/example/g8backend/service/PostServiceTest.java
@@ -2,6 +2,7 @@
import com.example.g8backend.entity.Post;
import com.example.g8backend.mapper.PostMapper;
+import com.example.g8backend.mapper.PostViewMapper;
import com.example.g8backend.service.impl.PostServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@@ -26,6 +27,9 @@
@Mock
private PostMapper postMapper;
+ @Mock
+ private PostViewMapper postViewMapper;
+
private PostServiceImpl postService;
private Post testPost;
@@ -33,7 +37,7 @@
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
- postService = new PostServiceImpl(postMapper);
+ postService = new PostServiceImpl(postMapper, postViewMapper);
testPost = createTestPost();
}