You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by ca...@apache.org on 2022/09/16 06:09:05 UTC

[incubator-linkis-website] branch dev updated: feat: Description of newly added single measurement analog data (#515)

This is an automated email from the ASF dual-hosted git repository.

casion pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-linkis-website.git


The following commit(s) were added to refs/heads/dev by this push:
     new 108e4ab145 feat: Description of newly added single measurement analog data (#515)
108e4ab145 is described below

commit 108e4ab145891b4f954744b8a3a92163e87cc6f8
Author: ruY <43...@users.noreply.github.com>
AuthorDate: Fri Sep 16 14:09:00 2022 +0800

    feat: Description of newly added single measurement analog data (#515)
---
 community/how-to-write-unit-test-code.md           | 35 +++++++++++++++++++++
 .../current/how-to-write-unit-test-code.md         | 36 ++++++++++++++++++++++
 2 files changed, 71 insertions(+)

diff --git a/community/how-to-write-unit-test-code.md b/community/how-to-write-unit-test-code.md
index 5c2f6791b5..9035cc1b48 100644
--- a/community/how-to-write-unit-test-code.md
+++ b/community/how-to-write-unit-test-code.md
@@ -264,6 +264,41 @@ assertEquals(2, jobRespProtocolArrayList.size());
 assertTrue(jobRespProtocolArrayList.stream(). anyMatch(statusPrecate));
 ```
 
+## Mock simulation return data
+
+Sometimes we just test some apis or service modules, where the service or dao returns null values for some methods by default, but if the logic includes the judgment or secondary value of the returned null object, it is to throw some exceptions
+
+Example:  
+
+```java
+    PageInfo<UDFAddVo> pageInfo =
+        udfService.getManagerPages(udfName, udfTypes, userName, curPage, pageSize);
+    message = Message.ok();
+    // The pageInfo here is null, and subsequent get methods will have exceptions
+    message.data("infoList", pageInfo.getList());
+    message.data("totalPage", pageInfo.getPages());
+    message.data("total", pageInfo.getTotal());
+```
+
+Example of mock simulation data:
+
+```java
+    PageInfo<UDFAddVo> pageInfo = new PageInfo<>();
+    pageInfo.setList(new ArrayList<>());
+    pageInfo.setPages(10);
+    pageInfo.setTotal(100);
+    // For udfService.getManagerPages method passes parameters arbitrarily, and the simulation returns the pageInfo object
+    // With this simulation data, the above example will not have exceptions when executing the get method
+    Mockito.when(
+            udfService.getManagerPages(
+                Mockito.anyString(),
+                Mockito.anyCollection(),
+                Mockito.anyString(),
+                Mockito.anyInt(),
+                Mockito.anyInt()))
+        .thenReturn(pageInfo);
+```
+
 ## Compilation of Unit Test
 
 ### Class Division
diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs-community/current/how-to-write-unit-test-code.md b/i18n/zh-CN/docusaurus-plugin-content-docs-community/current/how-to-write-unit-test-code.md
index 0bdc15ca36..6f7cd31e8b 100644
--- a/i18n/zh-CN/docusaurus-plugin-content-docs-community/current/how-to-write-unit-test-code.md
+++ b/i18n/zh-CN/docusaurus-plugin-content-docs-community/current/how-to-write-unit-test-code.md
@@ -235,9 +235,45 @@ Assertions.assertThrows方法,用来测试Executable实例执行execute方法
     assertTrue(jobRespProtocolArrayList.stream().anyMatch(statusPrecate));
   ```
 
+## Mock模拟返回数据
+
+有时我们单测一些api或者service模块,其中的service或者dao对于一些方法的返回值默认是null,但是逻辑里面有对这个返回null的对象进行判断或者二次取值的话,就是引发一些异常
+
+示例:
+
+```java
+    PageInfo<UDFAddVo> pageInfo =
+        udfService.getManagerPages(udfName, udfTypes, userName, curPage, pageSize);
+    message = Message.ok();
+    // 这里的pageInfo是null,后续的get方法就会出现异常
+    message.data("infoList", pageInfo.getList());
+    message.data("totalPage", pageInfo.getPages());
+    message.data("total", pageInfo.getTotal());
+```
+
+mock模拟数据示例:
+
+```java
+    PageInfo<UDFAddVo> pageInfo = new PageInfo<>();
+    pageInfo.setList(new ArrayList<>());
+    pageInfo.setPages(10);
+    pageInfo.setTotal(100);
+    // 对 udfService.getManagerPages 方法进行任意传递参数,模拟返回pageInfo对象
+    // 有了这个模拟数据,上面示例在执行get方法的时候,就不会有异常
+    Mockito.when(
+            udfService.getManagerPages(
+                Mockito.anyString(),
+                Mockito.anyCollection(),
+                Mockito.anyString(),
+                Mockito.anyInt(),
+                Mockito.anyInt()))
+        .thenReturn(pageInfo);
+```
 
 ## 单元测试的编写
+
 ### 类的划分
+
 按类的大功能可以大体分类
 - Controller  提供http服务的controller 配合mockmvc做单元测试 
 - Service   业务逻辑代码的service层


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org