Преглед изворни кода

添加 junit4+mockito 单元测试 demo

reghao пре 9 месеци
родитељ
комит
d56f5e026b

+ 13 - 0
content/content-service/pom.xml

@@ -157,6 +157,19 @@
             <version>1.12.1</version>
         </dependency>
 
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>3.7.7</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-boot-starter</artifactId>

+ 4 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/CommentService.java

@@ -152,4 +152,8 @@ public class CommentService {
                 .collect(Collectors.toList());
         return PageList.pageList(pageNumber, 10, total.intValue(), list);
     }
+
+    public long countPostComment(String videoId) {
+        return userCommentMongo.countByPostId(videoId);
+    }
 }

+ 32 - 0
content/content-service/src/test/java/cn/reghao/tnb/content/app/vod/service/UnitTest.java

@@ -0,0 +1,32 @@
+package cn.reghao.tnb.content.app.vod.service;
+
+import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
+import cn.reghao.tnb.content.app.vod.db.mongo.UserCommentMongo;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+/**
+ * @author reghao
+ * @date 2025-05-29 09:16:31
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class UnitTest {
+    @Mock
+    private UserCommentMongo userCommentMongo;
+    //@Mock
+    private VideoPostMapper videoPostMapper;
+    @InjectMocks
+    CommentService commentService;
+
+    @Test
+    public void unitTest() {
+        String videoId = "abcdefg";
+        Mockito.when(userCommentMongo.countByPostId(videoId)).thenReturn(1024L);
+        long total = commentService.countPostComment(videoId);
+        System.out.println();
+    }
+}