mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 18:28:43 +08:00 
			
		
		
		
	提交部分 config 的单元测试
This commit is contained in:
		| @@ -17,6 +17,8 @@ import java.util.function.Consumer; | ||||
|  | ||||
| import static cn.hutool.core.util.RandomUtil.randomEle; | ||||
| import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_KEY_DUPLICATE; | ||||
| import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS; | ||||
| import static cn.iocoder.dashboard.util.AssertUtils.assertExceptionEquals; | ||||
| import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||||
| import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| @@ -37,7 +39,7 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | ||||
|     @Test | ||||
|     public void testCreateConfig_success() { | ||||
|         // 准备参数 | ||||
|         InfConfigCreateReqVO reqVO = randomInfConfigCreateReqVO(); | ||||
|         InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class); | ||||
|         // mock | ||||
|  | ||||
|         // 调用 | ||||
| @@ -55,24 +57,28 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | ||||
|     @Test | ||||
|     public void testCreateConfig_keyDuplicate() { | ||||
|         // 准备参数 | ||||
|         InfConfigCreateReqVO reqVO = randomInfConfigCreateReqVO(); | ||||
|         InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class); | ||||
|         // mock 数据 | ||||
|         configMapper.insert(randomInfConfigDO(o -> { // @Sql | ||||
|             o.setKey(reqVO.getKey()); // 模拟 key 重复 | ||||
|         })); | ||||
|  | ||||
|         // 调用 | ||||
|         ServiceException serviceException = assertThrows(ServiceException.class, () -> configService.createConfig(reqVO)); | ||||
|         // 断言 | ||||
|         assertPojoEquals(CONFIG_KEY_DUPLICATE, serviceException); | ||||
|         ServiceException serviceException = assertThrows(ServiceException.class, | ||||
|                 () -> configService.createConfig(reqVO)); | ||||
|         // 断言异常 | ||||
|         assertExceptionEquals(CONFIG_KEY_DUPLICATE, serviceException); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateConfig_success() { | ||||
|         // 准备参数 | ||||
|         InfConfigUpdateReqVO reqVO = randomInfConfigUpdateReqVO(); | ||||
|         // mock 数据 | ||||
|         configMapper.insert(randomInfConfigDO(o -> o.setId(reqVO.getId())));// @Sql: 先插入出一条存在的数据 | ||||
|         InfConfigDO dbConfig = randomInfConfigDO(); | ||||
|         configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class, o -> { | ||||
|             o.setId(dbConfig.getId()); // 设置更新的 ID | ||||
|         }); | ||||
|  | ||||
|         // 调用 | ||||
|         configService.updateConfig(reqVO); | ||||
| @@ -83,21 +89,25 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | ||||
|         verify(configProducer, times(1)).sendConfigRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateConfig_notExists() { | ||||
|         // 准备参数 | ||||
|         InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class); | ||||
|  | ||||
|         // 调用 | ||||
|         ServiceException serviceException = assertThrows(ServiceException.class, | ||||
|                 () -> configService.updateConfig(reqVO)); | ||||
|         // 断言异常 | ||||
|         assertExceptionEquals(CONFIG_NOT_EXISTS, serviceException); | ||||
|     } | ||||
|  | ||||
|     // ========== 随机对象 ========== | ||||
|  | ||||
|     @SafeVarargs | ||||
|     private static InfConfigDO randomInfConfigDO(Consumer<InfConfigDO>... consumers) { | ||||
|         InfConfigDO config = randomPojo(InfConfigDO.class, consumers); | ||||
|         config.setType(randomEle(InfConfigTypeEnum.values()).getType()); | ||||
|         config.setType(randomEle(InfConfigTypeEnum.values()).getType()); // 保证 key 的范围 | ||||
|         return config; | ||||
|     } | ||||
|  | ||||
|     private static InfConfigCreateReqVO randomInfConfigCreateReqVO() { | ||||
|         return randomPojo(InfConfigCreateReqVO.class); | ||||
|     } | ||||
|  | ||||
|     private static InfConfigUpdateReqVO randomInfConfigUpdateReqVO() { | ||||
|         return randomPojo(InfConfigUpdateReqVO.class); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -52,7 +52,7 @@ public class AssertUtils { | ||||
|      * @param errorCode 错误码对象 | ||||
|      * @param serviceException 业务异常 | ||||
|      */ | ||||
|     public static void assertPojoEquals(ErrorCode errorCode, ServiceException serviceException) { | ||||
|     public static void assertExceptionEquals(ErrorCode errorCode, ServiceException serviceException) { | ||||
|         Assertions.assertEquals(errorCode.getCode(), serviceException.getCode(), "错误码不匹配"); | ||||
|         Assertions.assertEquals(errorCode.getMessage(), serviceException.getMessage(), "错误提示不匹配"); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV