单元测试,接入 h2 实现单元测试

This commit is contained in:
YunaiV
2021-02-28 13:29:24 +08:00
parent 99855e7cdc
commit 2f0d7e8aba
11 changed files with 371 additions and 49 deletions

View File

@ -0,0 +1,81 @@
package cn.iocoder.dashboard.modules.infra.service.config;
import cn.iocoder.dashboard.common.exception.ServiceException;
import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO;
import cn.iocoder.dashboard.modules.infra.dal.mysql.config.InfConfigMapper;
import cn.iocoder.dashboard.modules.infra.enums.config.InfConfigTypeEnum;
import cn.iocoder.dashboard.modules.infra.mq.producer.config.InfConfigProducer;
import cn.iocoder.dashboard.modules.infra.service.config.impl.InfConfigServiceImpl;
import cn.iocoder.dashboard.util.AssertUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import javax.annotation.Resource;
import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_KEY_DUPLICATE;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@SpringBootTest
@ActiveProfiles("unit-test")
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public class InfConfigServiceImplTest {
@Resource
private InfConfigServiceImpl configService;
@Resource
private InfConfigMapper configMapper;
@MockBean
private InfConfigProducer configProducer;
@Test
public void testCreateConfig_success() {
// 入参
InfConfigCreateReqVO reqVO = new InfConfigCreateReqVO();
reqVO.setGroup("test_group");
reqVO.setName("test_name");
reqVO.setValue("test_value");
reqVO.setSensitive(true);
reqVO.setRemark("test_remark");
reqVO.setKey("test_key");
// mock
// 调用
Long configId = configService.createConfig(reqVO);
// 校验
assertNotNull(configId);
// 校验记录的属性是否正确
InfConfigDO config = configMapper.selectById(configId);
AssertUtils.assertEquals(reqVO, config);
assertEquals(InfConfigTypeEnum.CUSTOM.getType(), config.getType());
// 校验调用
verify(configProducer, times(1)).sendConfigRefreshMessage();
}
@Test
@Sql(statements = "INSERT INTO `inf_config`(`group`, `type`, `name`, `key`, `value`, `sensitive`) VALUES ('test_group', 1, 'test_name', 'test_key', 'test_value', 1);")
public void testCreateConfig_keyDuplicate() {
// 入参
InfConfigCreateReqVO reqVO = new InfConfigCreateReqVO();
reqVO.setGroup("test_group");
reqVO.setName("test_name");
reqVO.setValue("test_value");
reqVO.setSensitive(true);
reqVO.setRemark("test_remark");
reqVO.setKey("test_key");
// mock
// 调用
ServiceException serviceException = assertThrows(ServiceException.class, () -> configService.createConfig(reqVO));
// 断言
AssertUtils.assertEquals(CONFIG_KEY_DUPLICATE, serviceException);
}
}

View File

@ -3,19 +3,23 @@ package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen;
import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
@ActiveProfiles("unit-test")
public class ToolInformationSchemaColumnMapperTest {
@Resource
private ToolSchemaColumnMapper toolInformationSchemaColumnMapper;
@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testSelectListByTableName() {
List<ToolSchemaColumnDO> columns = toolInformationSchemaColumnMapper
.selectListByTableName("inf_config");

View File

@ -0,0 +1,54 @@
package cn.iocoder.dashboard.util;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.dashboard.common.exception.ErrorCode;
import cn.iocoder.dashboard.common.exception.ServiceException;
import org.junit.jupiter.api.Assertions;
import java.lang.reflect.Field;
import java.util.Arrays;
/**
* 单元测试assert 断言工具类
*
* @author 芋道源码
*/
public class AssertUtils {
/**
* 比对两个对象的属性是否一致
*
* 注意,如果 expected 存在的属性actual 不存在的时候,会进行忽略
*
* @param expected 期望对象
* @param actual 实际对象
*/
public static void assertEquals(Object expected, Object actual) {
Field[] expectedFields = ReflectUtil.getFields(expected.getClass());
Arrays.stream(expectedFields).forEach(expectedField -> {
// 忽略不存在的属性
Field actualField = ReflectUtil.getField(actual.getClass(), expectedField.getName());
if (actualField == null) {
return;
}
// 比对
Assertions.assertEquals(
ReflectUtil.getFieldValue(expected, expectedField),
ReflectUtil.getFieldValue(actual, actualField),
String.format("Field(%s) 不匹配", expectedField.getName())
);
});
}
/**
* 比对抛出的 ServiceException 是否匹配
*
* @param errorCode 错误码对象
* @param serviceException 业务异常
*/
public static void assertEquals(ErrorCode errorCode, ServiceException serviceException) {
Assertions.assertEquals(errorCode.getCode(), serviceException.getCode(), "错误码不匹配");
Assertions.assertEquals(errorCode.getMessage(), serviceException.getMessage(), "错误提示不匹配");
}
}