From e5957f91339f71acb2be8ef452932451b165e221 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Sun, 26 Feb 2023 11:41:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test:=20=E5=AD=97=E5=85=B8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=A8=A1=E5=9D=97=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 6 +++ .../core/util/DictFrameworkUtilsTest.java | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java diff --git a/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml index 26e057d28..5acbc46c7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml @@ -39,5 +39,11 @@ com.google.guava guava + + + cn.iocoder.boot + yudao-spring-boot-starter-test + test + diff --git a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java new file mode 100644 index 000000000..fc98843e2 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java @@ -0,0 +1,53 @@ +package cn.iocoder.yudao.framework.dict.core.util; + +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; +import cn.iocoder.yudao.framework.test.core.util.RandomUtils; +import cn.iocoder.yudao.module.system.api.dict.DictDataApi; +import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +/** + * {@link DictFrameworkUtils} 的单元测试 + */ +public class DictFrameworkUtilsTest extends BaseMockitoUnitTest { + + @Mock + private DictDataApi dictDataApi; + + @BeforeEach + public void setUp() { + DictFrameworkUtils.init(dictDataApi); + } + + @Test + public void getDictDataLabelTest() { + DictDataRespDTO dataRespDTO = randomPojo(); + when(dictDataApi.getDictData(dataRespDTO.getDictType(), dataRespDTO.getValue())).thenReturn(dataRespDTO); + String dictDataLabel = DictFrameworkUtils.getDictDataLabel(dataRespDTO.getDictType(), dataRespDTO.getValue()); + assertEquals(dataRespDTO.getLabel(), dictDataLabel); + } + + @Test + public void parseDictDataValueTest() { + DictDataRespDTO resp = randomPojo(); + when(dictDataApi.parseDictData(resp.getDictType(), resp.getLabel())).thenReturn(resp); + String value = DictFrameworkUtils.parseDictDataValue(resp.getDictType(), resp.getLabel()); + assertEquals(resp.getValue(), value); + } + + private DictDataRespDTO randomPojo() { + DictDataRespDTO dataRespDTO = new DictDataRespDTO(); + dataRespDTO.setLabel(RandomUtils.randomString()); + dataRespDTO.setValue(RandomUtils.randomString()); + dataRespDTO.setDictType(RandomUtils.randomString()); + dataRespDTO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + + return dataRespDTO; + } +} From 2083fb78466938a3f0ca6250cbfc3b0139524e53 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Mon, 27 Feb 2023 22:29:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E4=BF=AE=E6=94=B9=20code=20review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 1 + .../core/util/DictFrameworkUtilsTest.java | 31 ++++++++----------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml index 5acbc46c7..e74551ff0 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-dict/pom.xml @@ -39,6 +39,7 @@ com.google.guava guava + cn.iocoder.boot diff --git a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java index fc98843e2..787cf2376 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java @@ -2,13 +2,13 @@ package cn.iocoder.yudao.framework.dict.core.util; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; -import cn.iocoder.yudao.framework.test.core.util.RandomUtils; import cn.iocoder.yudao.module.system.api.dict.DictDataApi; import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; @@ -26,28 +26,23 @@ public class DictFrameworkUtilsTest extends BaseMockitoUnitTest { } @Test - public void getDictDataLabelTest() { - DictDataRespDTO dataRespDTO = randomPojo(); + public void testGetDictDataLabel() { + // mock 数据 + DictDataRespDTO dataRespDTO = randomPojo(DictDataRespDTO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); + // mock 方法 when(dictDataApi.getDictData(dataRespDTO.getDictType(), dataRespDTO.getValue())).thenReturn(dataRespDTO); - String dictDataLabel = DictFrameworkUtils.getDictDataLabel(dataRespDTO.getDictType(), dataRespDTO.getValue()); - assertEquals(dataRespDTO.getLabel(), dictDataLabel); + // 断言返回值 + assertEquals(dataRespDTO.getLabel(), DictFrameworkUtils.getDictDataLabel(dataRespDTO.getDictType(), dataRespDTO.getValue())); } @Test - public void parseDictDataValueTest() { - DictDataRespDTO resp = randomPojo(); + public void testParseDictDataValue() { + // mock 数据 + DictDataRespDTO resp = randomPojo(DictDataRespDTO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); + // mock 方法 when(dictDataApi.parseDictData(resp.getDictType(), resp.getLabel())).thenReturn(resp); - String value = DictFrameworkUtils.parseDictDataValue(resp.getDictType(), resp.getLabel()); - assertEquals(resp.getValue(), value); + // 断言返回值 + assertEquals(resp.getValue(), DictFrameworkUtils.parseDictDataValue(resp.getDictType(), resp.getLabel())); } - private DictDataRespDTO randomPojo() { - DictDataRespDTO dataRespDTO = new DictDataRespDTO(); - dataRespDTO.setLabel(RandomUtils.randomString()); - dataRespDTO.setValue(RandomUtils.randomString()); - dataRespDTO.setDictType(RandomUtils.randomString()); - dataRespDTO.setStatus(CommonStatusEnum.ENABLE.getStatus()); - - return dataRespDTO; - } }