Commit c5ec798c10f1f0b50d6c95068d0af5f1dd273ef5
1 parent
2249d76d
简化 dict type 的单元测试,将新增、修改、删除涉及的公用方法,单独测试
Showing
3 changed files
with
83 additions
and
62 deletions
src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictTypeServiceImpl.java
| @@ -99,8 +99,8 @@ public class SysDictTypeServiceImpl implements SysDictTypeService { | @@ -99,8 +99,8 @@ public class SysDictTypeServiceImpl implements SysDictTypeService { | ||
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | @VisibleForTesting | 101 | @VisibleForTesting |
| 102 | - public void checkDictTypeNameUnique(Long id, String type) { | ||
| 103 | - SysDictTypeDO dictType = dictTypeMapper.selectByName(type); | 102 | + public void checkDictTypeNameUnique(Long id, String name) { |
| 103 | + SysDictTypeDO dictType = dictTypeMapper.selectByName(name); | ||
| 104 | if (dictType == null) { | 104 | if (dictType == null) { |
| 105 | return; | 105 | return; |
| 106 | } | 106 | } |
src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java
| @@ -205,6 +205,11 @@ public class SysDictDataServiceTest extends BaseSpringBootUnitTest { | @@ -205,6 +205,11 @@ public class SysDictDataServiceTest extends BaseSpringBootUnitTest { | ||
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | @Test | 207 | @Test |
| 208 | + public void testCheckDictDataExists_notExists() { | ||
| 209 | + assertServiceException(() -> dictDataService.checkDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + @Test | ||
| 208 | public void testCheckDictTypeValid_success() { | 213 | public void testCheckDictTypeValid_success() { |
| 209 | // mock 方法,数据类型被禁用 | 214 | // mock 方法,数据类型被禁用 |
| 210 | String type = randomString(); | 215 | String type = randomString(); |
src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java
| @@ -23,8 +23,8 @@ import static cn.hutool.core.util.RandomUtil.randomEle; | @@ -23,8 +23,8 @@ import static cn.hutool.core.util.RandomUtil.randomEle; | ||
| 23 | import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | 23 | import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; |
| 24 | import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | 24 | import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; |
| 25 | import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | 25 | import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; |
| 26 | -import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; | ||
| 27 | -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | 26 | +import static cn.iocoder.dashboard.util.RandomUtils.*; |
| 27 | +import static cn.iocoder.dashboard.util.RandomUtils.randomString; | ||
| 28 | import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; | 28 | import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; |
| 29 | import static org.junit.jupiter.api.Assertions.*; | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | import static org.mockito.ArgumentMatchers.eq; | 30 | import static org.mockito.ArgumentMatchers.eq; |
| @@ -143,32 +143,6 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { | @@ -143,32 +143,6 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { | ||
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | @Test | 145 | @Test |
| 146 | - public void testCreateDictType_nameDuplicate() { | ||
| 147 | - // mock 数据 | ||
| 148 | - SysDictTypeDO dbDictType = randomDictTypeDO(); | ||
| 149 | - dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 | ||
| 150 | - // 准备参数 | ||
| 151 | - SysDictTypeCreateReqVO reqVO = randomPojo(SysDictTypeCreateReqVO.class, | ||
| 152 | - o -> o.setName(dbDictType.getName())); // 模拟 name 重复 | ||
| 153 | - | ||
| 154 | - // 调用, 并断言异常 | ||
| 155 | - assertServiceException(() -> dictTypeService.createDictType(reqVO), DICT_TYPE_NAME_DUPLICATE); | ||
| 156 | - } | ||
| 157 | - | ||
| 158 | - @Test | ||
| 159 | - public void testCreateDictType_typeDuplicate() { | ||
| 160 | - // mock 数据 | ||
| 161 | - SysDictTypeDO dbDictType = randomDictTypeDO(); | ||
| 162 | - dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 | ||
| 163 | - // 准备参数 | ||
| 164 | - SysDictTypeCreateReqVO reqVO = randomPojo(SysDictTypeCreateReqVO.class, | ||
| 165 | - o -> o.setType(dbDictType.getType())); // 模拟 type 重复 | ||
| 166 | - | ||
| 167 | - // 调用, 并断言异常 | ||
| 168 | - assertServiceException(() -> dictTypeService.createDictType(reqVO), DICT_TYPE_TYPE_DUPLICATE); | ||
| 169 | - } | ||
| 170 | - | ||
| 171 | - @Test | ||
| 172 | public void testUpdateDictType_success() { | 146 | public void testUpdateDictType_success() { |
| 173 | // mock 数据 | 147 | // mock 数据 |
| 174 | SysDictTypeDO dbDictType = randomDictTypeDO(); | 148 | SysDictTypeDO dbDictType = randomDictTypeDO(); |
| @@ -187,67 +161,109 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { | @@ -187,67 +161,109 @@ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { | ||
| 187 | } | 161 | } |
| 188 | 162 | ||
| 189 | @Test | 163 | @Test |
| 190 | - public void testUpdateDictType_notExists() { | 164 | + public void testDeleteDictType_success() { |
| 165 | + // mock 数据 | ||
| 166 | + SysDictTypeDO dbDictType = randomDictTypeDO(); | ||
| 167 | + dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 | ||
| 191 | // 准备参数 | 168 | // 准备参数 |
| 192 | - SysDictTypeUpdateReqVO reqVO = randomPojo(SysDictTypeUpdateReqVO.class); | 169 | + Long id = dbDictType.getId(); |
| 193 | 170 | ||
| 194 | - // 调用, 并断言异常 | ||
| 195 | - assertServiceException(() -> dictTypeService.updateDictType(reqVO), DICT_TYPE_NOT_EXISTS); | 171 | + // 调用 |
| 172 | + dictTypeService.deleteDictType(id); | ||
| 173 | + // 校验数据不存在了 | ||
| 174 | + assertNull(dictTypeMapper.selectById(id)); | ||
| 196 | } | 175 | } |
| 197 | 176 | ||
| 198 | @Test | 177 | @Test |
| 199 | - public void testUpdateDictType_nameDuplicate() { | ||
| 200 | - // mock 数据,稍后更新它 | 178 | + public void testDeleteDictType_hasChildren() { |
| 179 | + // mock 数据 | ||
| 201 | SysDictTypeDO dbDictType = randomDictTypeDO(); | 180 | SysDictTypeDO dbDictType = randomDictTypeDO(); |
| 202 | - dictTypeMapper.insert(dbDictType); | ||
| 203 | - // mock 数据,ks稍后模拟重复它的名字 | ||
| 204 | - SysDictTypeDO nameDictType = randomDictTypeDO(); | ||
| 205 | - dictTypeMapper.insert(nameDictType); | 181 | + dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
| 206 | // 准备参数 | 182 | // 准备参数 |
| 207 | - SysDictTypeUpdateReqVO reqVO = randomPojo(SysDictTypeUpdateReqVO.class, o -> { | ||
| 208 | - o.setId(dbDictType.getId()); // 设置更新的 ID | ||
| 209 | - o.setName(nameDictType.getName()); // 模拟 name 重复 | ||
| 210 | - }); | 183 | + Long id = dbDictType.getId(); |
| 184 | + // mock 方法 | ||
| 185 | + when(dictDataService.countByDictType(eq(dbDictType.getType()))).thenReturn(1); | ||
| 211 | 186 | ||
| 212 | // 调用, 并断言异常 | 187 | // 调用, 并断言异常 |
| 213 | - assertServiceException(() -> dictTypeService.updateDictType(reqVO), DICT_TYPE_NAME_DUPLICATE); | 188 | + assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_HAS_CHILDREN); |
| 214 | } | 189 | } |
| 215 | 190 | ||
| 216 | @Test | 191 | @Test |
| 217 | - public void testDeleteDictType_success() { | 192 | + public void testCheckDictDataExists_success() { |
| 218 | // mock 数据 | 193 | // mock 数据 |
| 219 | SysDictTypeDO dbDictType = randomDictTypeDO(); | 194 | SysDictTypeDO dbDictType = randomDictTypeDO(); |
| 220 | dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 | 195 | dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
| 196 | + | ||
| 197 | + // 调用成功 | ||
| 198 | + dictTypeService.checkDictTypeExists(dbDictType.getId()); | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + @Test | ||
| 202 | + public void testCheckDictDataExists_notExists() { | ||
| 203 | + assertServiceException(() -> dictTypeService.checkDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS); | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + @Test | ||
| 207 | + public void testCheckDictTypeUnique_success() { | ||
| 208 | + // 调用,成功 | ||
| 209 | + dictTypeService.checkDictTypeUnique(randomLongId(), randomString()); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + @Test | ||
| 213 | + public void testCheckDictTypeUnique_valueDuplicateForCreate() { | ||
| 221 | // 准备参数 | 214 | // 准备参数 |
| 222 | - Long id = dbDictType.getId(); | 215 | + String type = randomString(); |
| 216 | + // mock 数据 | ||
| 217 | + dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); | ||
| 223 | 218 | ||
| 224 | - // 调用 | ||
| 225 | - dictTypeService.deleteDictType(id); | ||
| 226 | - // 校验数据不存在了 | ||
| 227 | - assertNull(dictTypeMapper.selectById(id)); | 219 | + // 调用,校验异常 |
| 220 | + assertServiceException(() -> dictTypeService.checkDictTypeUnique(null, type), | ||
| 221 | + DICT_TYPE_TYPE_DUPLICATE); | ||
| 228 | } | 222 | } |
| 229 | 223 | ||
| 230 | @Test | 224 | @Test |
| 231 | - public void testDeleteDictType_notExists() { | 225 | + public void testCheckDictTypeUnique_valueDuplicateForUpdate() { |
| 232 | // 准备参数 | 226 | // 准备参数 |
| 233 | Long id = randomLongId(); | 227 | Long id = randomLongId(); |
| 228 | + String type = randomString(); | ||
| 229 | + // mock 数据 | ||
| 230 | + dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); | ||
| 234 | 231 | ||
| 235 | - // 调用, 并断言异常 | ||
| 236 | - assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_NOT_EXISTS); | 232 | + // 调用,校验异常 |
| 233 | + assertServiceException(() -> dictTypeService.checkDictTypeUnique(id, type), | ||
| 234 | + DICT_TYPE_TYPE_DUPLICATE); | ||
| 237 | } | 235 | } |
| 238 | 236 | ||
| 237 | + | ||
| 239 | @Test | 238 | @Test |
| 240 | - public void testDeleteDictType_hasChildren() { | 239 | + public void testCheckDictTypNameUnique_success() { |
| 240 | + // 调用,成功 | ||
| 241 | + dictTypeService.checkDictTypeNameUnique(randomLongId(), randomString()); | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + @Test | ||
| 245 | + public void testCheckDictTypeNameUnique_valueDuplicateForCreate() { | ||
| 246 | + // 准备参数 | ||
| 247 | + String name = randomString(); | ||
| 241 | // mock 数据 | 248 | // mock 数据 |
| 242 | - SysDictTypeDO dbDictType = randomDictTypeDO(); | ||
| 243 | - dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 | 249 | + dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); |
| 250 | + | ||
| 251 | + // 调用,校验异常 | ||
| 252 | + assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(null, name), | ||
| 253 | + DICT_TYPE_NAME_DUPLICATE); | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + @Test | ||
| 257 | + public void testCheckDictTypeNameUnique_valueDuplicateForUpdate() { | ||
| 244 | // 准备参数 | 258 | // 准备参数 |
| 245 | - Long id = dbDictType.getId(); | ||
| 246 | - // mock 方法 | ||
| 247 | - when(dictDataService.countByDictType(eq(dbDictType.getType()))).thenReturn(1); | 259 | + Long id = randomLongId(); |
| 260 | + String name = randomString(); | ||
| 261 | + // mock 数据 | ||
| 262 | + dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); | ||
| 248 | 263 | ||
| 249 | - // 调用, 并断言异常 | ||
| 250 | - assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_HAS_CHILDREN); | 264 | + // 调用,校验异常 |
| 265 | + assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(id, name), | ||
| 266 | + DICT_TYPE_NAME_DUPLICATE); | ||
| 251 | } | 267 | } |
| 252 | 268 | ||
| 253 | // ========== 随机对象 ========== | 269 | // ========== 随机对象 ========== |