Commit 01be94a7a062633884c9947d64b961b1f056e241

Authored by YunaiV
1 parent 189a995d

简化 config 的单元测试,将新增、修改、删除涉及的公用方法,单独测试

sql/ruoyi-vue-pro.sql
... ... @@ -11,7 +11,7 @@
11 11 Target Server Version : 50718
12 12 File Encoding : 65001
13 13  
14   - Date: 07/03/2021 00:43:34
  14 + Date: 08/03/2021 00:50:29
15 15 */
16 16  
17 17 SET NAMES utf8mb4;
... ... @@ -43,7 +43,7 @@ CREATE TABLE `inf_api_access_log` (
43 43 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ›´æ–°æ—¶é—´',
44 44 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是å¦åˆ é™¤',
45 45 PRIMARY KEY (`id`) USING BTREE
46   -) ENGINE=InnoDB AUTO_INCREMENT=1822 DEFAULT CHARSET=utf8mb4 COMMENT='API 访问日志表';
  46 +) ENGINE=InnoDB AUTO_INCREMENT=1850 DEFAULT CHARSET=utf8mb4 COMMENT='API 访问日志表';
47 47  
48 48 -- ----------------------------
49 49 -- Records of inf_api_access_log
... ... @@ -175,7 +175,7 @@ CREATE TABLE `inf_job_log` (
175 175 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ›´æ–°æ—¶é—´',
176 176 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是å¦åˆ é™¤',
177 177 PRIMARY KEY (`id`) USING BTREE
178   -) ENGINE=InnoDB AUTO_INCREMENT=4458 DEFAULT CHARSET=utf8mb4 COMMENT='定时任务日志表';
  178 +) ENGINE=InnoDB AUTO_INCREMENT=4477 DEFAULT CHARSET=utf8mb4 COMMENT='定时任务日志表';
179 179  
180 180 -- ----------------------------
181 181 -- Records of inf_job_log
... ... @@ -579,7 +579,7 @@ CREATE TABLE `sys_operate_log` (
579 579 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ›´æ–°æ—¶é—´',
580 580 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是å¦åˆ é™¤',
581 581 PRIMARY KEY (`id`) USING BTREE
582   -) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COMMENT='æ“作日志记录';
  582 +) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COMMENT='æ“作日志记录';
583 583  
584 584 -- ----------------------------
585 585 -- Records of sys_operate_log
... ... @@ -955,7 +955,7 @@ INSERT INTO `sys_user_session` VALUES ('7b008d71139f48a4903aeaab0bbbef2e', 1, '1
955 955 INSERT INTO `sys_user_session` VALUES ('7cbd0fdfb9304f298abb9ee2f58f7230', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-02-08 20:02:24', '', '2021-02-08 20:02:24', b'0');
956 956 INSERT INTO `sys_user_session` VALUES ('830a1687977b44969025177ad3b6d051', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-02-10 01:56:00', '', '2021-02-10 01:56:00', b'0');
957 957 INSERT INTO `sys_user_session` VALUES ('8331dbbca7be4348a3d2d02bf559b65e', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-02-05 13:29:04', '', '2021-02-10 19:06:26', b'0');
958   -INSERT INTO `sys_user_session` VALUES ('897714de63634dba9ac235247f9860c2', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-03-06 06:27:27', '', '2021-03-06 06:27:27', b'0');
  958 +INSERT INTO `sys_user_session` VALUES ('897714de63634dba9ac235247f9860c2', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-03-06 06:27:27', '', '2021-03-07 00:50:21', b'0');
959 959 INSERT INTO `sys_user_session` VALUES ('8d7668c1276344ecbffaf7e4420595d7', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-02-08 09:56:09', '', '2021-02-19 19:17:42', b'0');
960 960 INSERT INTO `sys_user_session` VALUES ('9a3a5b8dee1841b69d70454ce23a53a3', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-02-26 00:09:49', '', '2021-02-27 01:27:20', b'0');
961 961 INSERT INTO `sys_user_session` VALUES ('9cc71dc2d7a24b978db1bfe0e4bae349', 1, '127.0.0.1', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36', '', '2021-01-26 08:30:01', '', '2021-01-26 08:30:01', b'0');
... ... @@ -1006,7 +1006,7 @@ CREATE TABLE `tool_codegen_column` (
1006 1006 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ›´æ–°æ—¶é—´',
1007 1007 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是å¦åˆ é™¤',
1008 1008 PRIMARY KEY (`id`) USING BTREE
1009   -) ENGINE=InnoDB AUTO_INCREMENT=369 DEFAULT CHARSET=utf8mb4 COMMENT='代ç ç”Ÿæˆè¡¨å­—段定义';
  1009 +) ENGINE=InnoDB AUTO_INCREMENT=381 DEFAULT CHARSET=utf8mb4 COMMENT='代ç ç”Ÿæˆè¡¨å­—段定义';
1010 1010  
1011 1011 -- ----------------------------
1012 1012 -- Records of tool_codegen_column
... ... @@ -1148,6 +1148,18 @@ INSERT INTO `tool_codegen_column` VALUES (365, 29, 'create_time', 'datetime', 'å
1148 1148 INSERT INTO `tool_codegen_column` VALUES (366, 29, 'update_by', 'varchar(64)', '更新者', b'1', b'0', '0', 8, 'String', 'updateBy', '', NULL, b'0', b'0', b'0', '=', b'0', 'input', '', '2021-03-06 03:52:57', '', '2021-03-06 03:52:57', b'0');
1149 1149 INSERT INTO `tool_codegen_column` VALUES (367, 29, 'update_time', 'datetime', 'æ›´æ–°æ—¶é—´', b'0', b'0', '0', 9, 'Date', 'updateTime', '', NULL, b'0', b'0', b'0', 'BETWEEN', b'0', 'datetime', '', '2021-03-06 03:52:57', '', '2021-03-06 03:52:57', b'0');
1150 1150 INSERT INTO `tool_codegen_column` VALUES (368, 29, 'deleted', 'bit(1)', '是å¦åˆ é™¤', b'0', b'0', '0', 10, 'Boolean', 'deleted', '', NULL, b'0', b'0', b'0', '=', b'0', 'radio', '', '2021-03-06 03:52:57', '', '2021-03-06 03:52:57', b'0');
  1151 +INSERT INTO `tool_codegen_column` VALUES (369, 30, 'id', 'bigint(20)', '字典编ç ', b'0', b'1', '1', 1, 'Long', 'id', '', NULL, b'0', b'1', b'0', '=', b'1', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:48:28', b'0');
  1152 +INSERT INTO `tool_codegen_column` VALUES (370, 30, 'sort', 'int(4)', '字典排åº', b'0', b'0', '0', 2, 'Integer', 'sort', '', NULL, b'1', b'1', b'0', '=', b'1', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:38', b'0');
  1153 +INSERT INTO `tool_codegen_column` VALUES (371, 30, 'label', 'varchar(100)', '字典标签', b'0', b'0', '0', 3, 'String', 'label', '', NULL, b'1', b'1', b'1', 'LIKE', b'1', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:38', b'0');
  1154 +INSERT INTO `tool_codegen_column` VALUES (372, 30, 'value', 'varchar(100)', '字典键值', b'0', b'0', '0', 4, 'String', 'value', '', NULL, b'1', b'1', b'0', '=', b'1', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:38', b'0');
  1155 +INSERT INTO `tool_codegen_column` VALUES (373, 30, 'dict_type', 'varchar(100)', '字典类型', b'0', b'0', '0', 5, 'String', 'dictType', '', NULL, b'1', b'1', b'1', 'LIKE', b'1', 'select', '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:38', b'0');
  1156 +INSERT INTO `tool_codegen_column` VALUES (374, 30, 'status', 'tinyint(4)', '状æ€ï¼ˆ0正常 1åœç”¨ï¼‰', b'0', b'0', '0', 6, 'Integer', 'status', '', NULL, b'1', b'1', b'1', 'LIKE', b'1', 'radio', '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:38', b'0');
  1157 +INSERT INTO `tool_codegen_column` VALUES (375, 30, 'remark', 'varchar(500)', '备注', b'1', b'0', '0', 7, 'String', 'remark', '', NULL, b'1', b'1', b'0', '=', b'1', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:38', b'0');
  1158 +INSERT INTO `tool_codegen_column` VALUES (376, 30, 'create_by', 'varchar(64)', '创建者', b'1', b'0', '0', 8, 'String', 'createBy', '', NULL, b'0', b'0', b'0', '=', b'0', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:48:28', b'0');
  1159 +INSERT INTO `tool_codegen_column` VALUES (377, 30, 'create_time', 'datetime', '创建时间', b'0', b'0', '0', 9, 'Date', 'createTime', '', NULL, b'0', b'0', b'1', 'BETWEEN', b'1', 'datetime', '', '2021-03-06 06:48:28', '', '2021-03-06 06:48:28', b'0');
  1160 +INSERT INTO `tool_codegen_column` VALUES (378, 30, 'update_by', 'varchar(64)', '更新者', b'1', b'0', '0', 10, 'String', 'updateBy', '', NULL, b'0', b'0', b'0', '=', b'0', 'input', '', '2021-03-06 06:48:28', '', '2021-03-06 06:48:28', b'0');
  1161 +INSERT INTO `tool_codegen_column` VALUES (379, 30, 'update_time', 'datetime', 'æ›´æ–°æ—¶é—´', b'0', b'0', '0', 11, 'Date', 'updateTime', '', NULL, b'0', b'0', b'0', 'BETWEEN', b'0', 'datetime', '', '2021-03-06 06:48:28', '', '2021-03-06 06:48:28', b'0');
  1162 +INSERT INTO `tool_codegen_column` VALUES (380, 30, 'deleted', 'bit(1)', '是å¦åˆ é™¤', b'0', b'0', '0', 12, 'Boolean', 'deleted', '', NULL, b'0', b'0', b'0', '=', b'0', 'radio', '', '2021-03-06 06:48:28', '', '2021-03-06 06:48:28', b'0');
1151 1163 COMMIT;
1152 1164  
1153 1165 -- ----------------------------
... ... @@ -1173,7 +1185,7 @@ CREATE TABLE `tool_codegen_table` (
1173 1185 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ›´æ–°æ—¶é—´',
1174 1186 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是å¦åˆ é™¤',
1175 1187 PRIMARY KEY (`id`) USING BTREE
1176   -) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8mb4 COMMENT='代ç ç”Ÿæˆè¡¨å®šä¹‰';
  1188 +) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COMMENT='代ç ç”Ÿæˆè¡¨å®šä¹‰';
1177 1189  
1178 1190 -- ----------------------------
1179 1191 -- Records of tool_codegen_table
... ... @@ -1189,6 +1201,7 @@ INSERT INTO `tool_codegen_table` VALUES (26, 1, 'inf_api_access_log', 'API 访é—
1189 1201 INSERT INTO `tool_codegen_table` VALUES (27, 1, 'inf_api_error_log', 'API 错误日志', NULL, 'infra', 'apiErrorLog', 'InfApiErrorLog', 'API 错误日志', 'èŠ‹é“æºç ', 1, 1083, '', '2021-02-26 06:54:49', '', '2021-02-26 07:53:03', b'0');
1190 1202 INSERT INTO `tool_codegen_table` VALUES (28, 1, 'sys_dict_type', '字典类型表', NULL, 'system', 'dictType', 'SysDictType', '字典类型', '芋艿', 1, NULL, '', '2021-03-06 03:45:55', '', '2021-03-06 03:51:02', b'1');
1191 1203 INSERT INTO `tool_codegen_table` VALUES (29, 1, 'sys_dict_type', '字典类型表', NULL, 'system', 'dict', 'SysDictType', '字典类型', '芋艿', 1, NULL, '', '2021-03-06 03:52:57', '', '2021-03-06 04:03:52', b'0');
  1204 +INSERT INTO `tool_codegen_table` VALUES (30, 1, 'sys_dict_data', '字典数æ®è¡¨', NULL, 'system', 'type', 'SysDictData', '字典数æ®', 'èŠ‹é“æºç ', 1, NULL, '', '2021-03-06 06:48:28', '', '2021-03-06 06:50:47', b'0');
1192 1205 COMMIT;
1193 1206  
1194 1207 -- ----------------------------
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java
... ... @@ -12,6 +12,7 @@ import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO;
12 12 import cn.iocoder.dashboard.modules.infra.enums.config.InfConfigTypeEnum;
13 13 import cn.iocoder.dashboard.modules.infra.mq.producer.config.InfConfigProducer;
14 14 import cn.iocoder.dashboard.modules.infra.service.config.InfConfigService;
  15 +import com.google.common.annotations.VisibleForTesting;
15 16 import lombok.extern.slf4j.Slf4j;
16 17 import org.springframework.stereotype.Service;
17 18  
... ... @@ -99,7 +100,8 @@ public class InfConfigServiceImpl implements InfConfigService {
99 100 checkConfigKeyUnique(id, key);
100 101 }
101 102  
102   - private InfConfigDO checkConfigExists(Long id) {
  103 + @VisibleForTesting
  104 + public InfConfigDO checkConfigExists(Long id) {
103 105 if (id == null) {
104 106 return null;
105 107 }
... ... @@ -110,7 +112,8 @@ public class InfConfigServiceImpl implements InfConfigService {
110 112 return config;
111 113 }
112 114  
113   - private void checkConfigKeyUnique(Long id, String key) {
  115 + @VisibleForTesting
  116 + public void checkConfigKeyUnique(Long id, String key) {
114 117 InfConfigDO config = configMapper.selectByKey(key);
115 118 if (config == null) {
116 119 return;
... ...
src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java
... ... @@ -24,8 +24,7 @@ import static cn.hutool.core.util.RandomUtil.randomEle;
24 24 import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.*;
25 25 import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
26 26 import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
27   -import static cn.iocoder.dashboard.util.RandomUtils.randomLongId;
28   -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
  27 +import static cn.iocoder.dashboard.util.RandomUtils.*;
29 28 import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
30 29 import static org.junit.jupiter.api.Assertions.*;
31 30 import static org.mockito.Mockito.times;
... ... @@ -146,19 +145,6 @@ public class InfConfigServiceTest extends BaseSpringBootUnitTest {
146 145 }
147 146  
148 147 @Test
149   - public void testCreateConfig_keyDuplicate() {
150   - // 准备参数
151   - InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class);
152   - // mock 数据
153   - configMapper.insert(randomInfConfigDO(o -> { // @Sql
154   - o.setKey(reqVO.getKey()); // 模拟 key 重复
155   - }));
156   -
157   - // 调用, 并断言异常
158   - assertServiceException(() -> configService.createConfig(reqVO), CONFIG_KEY_DUPLICATE);
159   - }
160   -
161   - @Test
162 148 public void testUpdateConfig_success() {
163 149 // mock 数据
164 150 InfConfigDO dbConfig = randomInfConfigDO();
... ... @@ -178,15 +164,6 @@ public class InfConfigServiceTest extends BaseSpringBootUnitTest {
178 164 }
179 165  
180 166 @Test
181   - public void testUpdateConfig_notExists() {
182   - // 准备参数
183   - InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class);
184   -
185   - // 调用, 并断言异常
186   - assertServiceException(() -> configService.updateConfig(reqVO), CONFIG_NOT_EXISTS);
187   - }
188   -
189   - @Test
190 167 public void testDeleteConfig_success() {
191 168 // mock 数据
192 169 InfConfigDO dbConfig = randomInfConfigDO(o -> {
... ... @@ -219,12 +196,49 @@ public class InfConfigServiceTest extends BaseSpringBootUnitTest {
219 196 }
220 197  
221 198 @Test
222   - public void testDeleteConfig_notExists() {
  199 + public void testCheckConfigExists_success() {
  200 + // mock 数据
  201 + InfConfigDO dbConfigDO = randomInfConfigDO();
  202 + configMapper.insert(dbConfigDO);// @Sql: 先插入出一条存在的数据
  203 +
  204 + // 调用成功
  205 + configService.checkConfigExists(dbConfigDO.getId());
  206 + }
  207 +
  208 + @Test
  209 + public void testCheckConfigExist_notExists() {
  210 + assertServiceException(() -> configService.checkConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
  211 + }
  212 +
  213 + @Test
  214 + public void testCheckConfigKeyUnique_success() {
  215 + // 调用,成功
  216 + configService.checkConfigKeyUnique(randomLongId(), randomString());
  217 + }
  218 +
  219 + @Test
  220 + public void testCheckConfigKeyUnique_keyDuplicateForCreate() {
  221 + // 准备参数
  222 + String key = randomString();
  223 + // mock 数据
  224 + configMapper.insert(randomInfConfigDO(o -> o.setKey(key)));
  225 +
  226 + // 调用,校验异常
  227 + assertServiceException(() -> configService.checkConfigKeyUnique(null, key),
  228 + CONFIG_KEY_DUPLICATE);
  229 + }
  230 +
  231 + @Test
  232 + public void testCheckConfigKeyUnique_keyDuplicateForUpdate() {
223 233 // 准备参数
224 234 Long id = randomLongId();
  235 + String key = randomString();
  236 + // mock 数据
  237 + configMapper.insert(randomInfConfigDO(o -> o.setKey(key)));
225 238  
226   - // 调用, 并断言异常
227   - assertServiceException(() -> configService.deleteConfig(id), CONFIG_NOT_EXISTS);
  239 + // 调用,校验异常
  240 + assertServiceException(() -> configService.checkConfigKeyUnique(id, key),
  241 + CONFIG_KEY_DUPLICATE);
228 242 }
229 243  
230 244 // ========== 随机对象 ==========
... ...
src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java
... ... @@ -234,7 +234,6 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest {
234 234 DICT_TYPE_TYPE_DUPLICATE);
235 235 }
236 236  
237   -
238 237 @Test
239 238 public void testCheckDictTypNameUnique_success() {
240 239 // 调用,成功
... ... @@ -242,7 +241,7 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest {
242 241 }
243 242  
244 243 @Test
245   - public void testCheckDictTypeNameUnique_valueDuplicateForCreate() {
  244 + public void testCheckDictTypeNameUnique_nameDuplicateForCreate() {
246 245 // 准备参数
247 246 String name = randomString();
248 247 // mock 数据
... ... @@ -254,7 +253,7 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest {
254 253 }
255 254  
256 255 @Test
257   - public void testCheckDictTypeNameUnique_valueDuplicateForUpdate() {
  256 + public void testCheckDictTypeNameUnique_nameDuplicateForUpdate() {
258 257 // 准备参数
259 258 Long id = randomLongId();
260 259 String name = randomString();
... ...