提交部分 config 的单元测试

This commit is contained in:
YunaiV 2021-03-06 02:39:21 +08:00
parent ba34e3c987
commit 72817a8632
4 changed files with 30 additions and 20 deletions

View File

@ -10,7 +10,7 @@ import cn.iocoder.dashboard.common.exception.ErrorCode;
public interface InfErrorCodeConstants {
// ========== 参数配置 1001000000 ==========
ErrorCode CONFIG_NOT_FOUND = new ErrorCode(1001000001, "参数配置不存在");
ErrorCode CONFIG_NOT_EXISTS = new ErrorCode(1001000001, "参数配置不存在");
ErrorCode CONFIG_KEY_DUPLICATE = new ErrorCode(1001000002, "参数配置 key 重复");
ErrorCode CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE = new ErrorCode(1001000003, "不能删除类型为系统内置的参数配置");
ErrorCode CONFIG_GET_VALUE_ERROR_IF_SENSITIVE = new ErrorCode(1001000004, "不允许获取敏感配置到前端");

View File

@ -105,7 +105,7 @@ public class InfConfigServiceImpl implements InfConfigService {
}
InfConfigDO config = configMapper.selectById(id);
if (config == null) {
throw ServiceExceptionUtil.exception(CONFIG_NOT_FOUND);
throw ServiceExceptionUtil.exception(CONFIG_NOT_EXISTS);
}
return config;
}

View File

@ -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);
}
}

View File

@ -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(), "错误提示不匹配");
}