Commit 72817a8632dd4ab58381ff2686079672178de28d

Authored by YunaiV
1 parent ba34e3c9

提交部分 config 的单元测试

src/main/java/cn/iocoder/dashboard/modules/infra/enums/InfErrorCodeConstants.java
... ... @@ -10,7 +10,7 @@ import cn.iocoder.dashboard.common.exception.ErrorCode;
10 10 public interface InfErrorCodeConstants {
11 11  
12 12 // ========== 参数配置 1001000000 ==========
13   - ErrorCode CONFIG_NOT_FOUND = new ErrorCode(1001000001, "参数配置不存在");
  13 + ErrorCode CONFIG_NOT_EXISTS = new ErrorCode(1001000001, "参数配置不存在");
14 14 ErrorCode CONFIG_KEY_DUPLICATE = new ErrorCode(1001000002, "参数配置 key 重复");
15 15 ErrorCode CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE = new ErrorCode(1001000003, "不能删除类型为系统内置的参数配置");
16 16 ErrorCode CONFIG_GET_VALUE_ERROR_IF_SENSITIVE = new ErrorCode(1001000004, "不允许获取敏感配置到前端");
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java
... ... @@ -105,7 +105,7 @@ public class InfConfigServiceImpl implements InfConfigService {
105 105 }
106 106 InfConfigDO config = configMapper.selectById(id);
107 107 if (config == null) {
108   - throw ServiceExceptionUtil.exception(CONFIG_NOT_FOUND);
  108 + throw ServiceExceptionUtil.exception(CONFIG_NOT_EXISTS);
109 109 }
110 110 return config;
111 111 }
... ...
src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java
... ... @@ -17,6 +17,8 @@ import java.util.function.Consumer;
17 17  
18 18 import static cn.hutool.core.util.RandomUtil.randomEle;
19 19 import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_KEY_DUPLICATE;
  20 +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS;
  21 +import static cn.iocoder.dashboard.util.AssertUtils.assertExceptionEquals;
20 22 import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
21 23 import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
22 24 import static org.junit.jupiter.api.Assertions.*;
... ... @@ -37,7 +39,7 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
37 39 @Test
38 40 public void testCreateConfig_success() {
39 41 // 准备参数
40   - InfConfigCreateReqVO reqVO = randomInfConfigCreateReqVO();
  42 + InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class);
41 43 // mock
42 44  
43 45 // 调用
... ... @@ -55,24 +57,28 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
55 57 @Test
56 58 public void testCreateConfig_keyDuplicate() {
57 59 // 准备参数
58   - InfConfigCreateReqVO reqVO = randomInfConfigCreateReqVO();
  60 + InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class);
59 61 // mock 数据
60 62 configMapper.insert(randomInfConfigDO(o -> { // @Sql
61 63 o.setKey(reqVO.getKey()); // 模拟 key 重复
62 64 }));
63 65  
64 66 // 调用
65   - ServiceException serviceException = assertThrows(ServiceException.class, () -> configService.createConfig(reqVO));
66   - // 断言
67   - assertPojoEquals(CONFIG_KEY_DUPLICATE, serviceException);
  67 + ServiceException serviceException = assertThrows(ServiceException.class,
  68 + () -> configService.createConfig(reqVO));
  69 + // 断言异常
  70 + assertExceptionEquals(CONFIG_KEY_DUPLICATE, serviceException);
68 71 }
69 72  
70 73 @Test
71 74 public void testUpdateConfig_success() {
72   - // 准备参数
73   - InfConfigUpdateReqVO reqVO = randomInfConfigUpdateReqVO();
74 75 // mock 数据
75   - configMapper.insert(randomInfConfigDO(o -> o.setId(reqVO.getId())));// @Sql: 先插入出一条存在的数据
  76 + InfConfigDO dbConfig = randomInfConfigDO();
  77 + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
  78 + // 准备参数
  79 + InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class, o -> {
  80 + o.setId(dbConfig.getId()); // 设置更新的 ID
  81 + });
76 82  
77 83 // 调用
78 84 configService.updateConfig(reqVO);
... ... @@ -83,21 +89,25 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
83 89 verify(configProducer, times(1)).sendConfigRefreshMessage();
84 90 }
85 91  
  92 + @Test
  93 + public void testUpdateConfig_notExists() {
  94 + // 准备参数
  95 + InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class);
  96 +
  97 + // 调用
  98 + ServiceException serviceException = assertThrows(ServiceException.class,
  99 + () -> configService.updateConfig(reqVO));
  100 + // 断言异常
  101 + assertExceptionEquals(CONFIG_NOT_EXISTS, serviceException);
  102 + }
  103 +
86 104 // ========== 随机对象 ==========
87 105  
88 106 @SafeVarargs
89 107 private static InfConfigDO randomInfConfigDO(Consumer<InfConfigDO>... consumers) {
90 108 InfConfigDO config = randomPojo(InfConfigDO.class, consumers);
91   - config.setType(randomEle(InfConfigTypeEnum.values()).getType());
  109 + config.setType(randomEle(InfConfigTypeEnum.values()).getType()); // 保证 key 的范围
92 110 return config;
93 111 }
94 112  
95   - private static InfConfigCreateReqVO randomInfConfigCreateReqVO() {
96   - return randomPojo(InfConfigCreateReqVO.class);
97   - }
98   -
99   - private static InfConfigUpdateReqVO randomInfConfigUpdateReqVO() {
100   - return randomPojo(InfConfigUpdateReqVO.class);
101   - }
102   -
103 113 }
... ...
src/test/java/cn/iocoder/dashboard/util/AssertUtils.java
... ... @@ -52,7 +52,7 @@ public class AssertUtils {
52 52 * @param errorCode 错误码对象
53 53 * @param serviceException 业务异常
54 54 */
55   - public static void assertPojoEquals(ErrorCode errorCode, ServiceException serviceException) {
  55 + public static void assertExceptionEquals(ErrorCode errorCode, ServiceException serviceException) {
56 56 Assertions.assertEquals(errorCode.getCode(), serviceException.getCode(), "错误码不匹配");
57 57 Assertions.assertEquals(errorCode.getMessage(), serviceException.getMessage(), "错误提示不匹配");
58 58 }
... ...