单元测试,接入 h2 实现单元测试
parent
99855e7cdc
commit
2f0d7e8aba
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,44 +0,0 @@
|
|||||||
spring:
|
|
||||||
application:
|
|
||||||
name: dashboard
|
|
||||||
|
|
||||||
profiles:
|
|
||||||
active: local
|
|
||||||
|
|
||||||
# Servlet 配置
|
|
||||||
servlet:
|
|
||||||
# 文件上传相关配置项
|
|
||||||
multipart:
|
|
||||||
max-file-size: 16MB # 单个文件大小
|
|
||||||
max-request-size: 32MB # 设置总上传的文件大小
|
|
||||||
|
|
||||||
# Jackson 配置项
|
|
||||||
jackson:
|
|
||||||
serialization:
|
|
||||||
write-dates-as-timestamps: true # 设置 Date 的格式,使用时间戳
|
|
||||||
write-date-timestamps-as-nanoseconds: false # 设置不使用 nanoseconds 的格式。例如说 1611460870.401,而是直接 1611460870401
|
|
||||||
write-durations-as-timestamps: true # 设置 Duration 的格式,使用时间戳
|
|
||||||
fail-on-empty-beans: false # 允许序列化无属性的 Bean
|
|
||||||
|
|
||||||
main:
|
|
||||||
lazy-initialization: true
|
|
||||||
|
|
||||||
# 去除的自动配置项
|
|
||||||
autoconfigure:
|
|
||||||
exclude:
|
|
||||||
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
|
|
||||||
- org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration
|
|
||||||
- org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration
|
|
||||||
|
|
||||||
# MyBatis Plus 的配置项
|
|
||||||
mybatis-plus:
|
|
||||||
configuration:
|
|
||||||
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。
|
|
||||||
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印日志
|
|
||||||
global-config:
|
|
||||||
db-config:
|
|
||||||
id-type: AUTO # 自增 ID
|
|
||||||
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
|
||||||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
|
||||||
mapper-locations: classpath*:mapper/*.xml
|
|
||||||
type-aliases-package: ${yudao.info.base-package}.modules.*.dal.dataobject
|
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
-- inf 开头的 DB
|
||||||
|
DELETE FROM "inf_config";
|
||||||
|
|
||||||
|
-- sys 开头的 DB
|
||||||
|
DELETE FROM "sys_dept";
|
||||||
|
DELETE FROM "sys_dict_data";
|
||||||
|
DELETE FROM "sys_role";
|
||||||
|
DELETE FROM "sys_role_menu";
|
||||||
|
DELETE FROM "sys_menu";
|
||||||
@ -0,0 +1,102 @@
|
|||||||
|
-- inf 开头的 DB
|
||||||
|
|
||||||
|
CREATE TABLE "inf_config" (
|
||||||
|
"id" int NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"group" varchar(50) NOT NULL,
|
||||||
|
"type" tinyint NOT NULL,
|
||||||
|
"name" varchar(100) NOT NULL DEFAULT '',
|
||||||
|
"key" varchar(100) NOT NULL DEFAULT '',
|
||||||
|
"value" varchar(500) NOT NULL DEFAULT '',
|
||||||
|
"sensitive" bit NOT NULL,
|
||||||
|
"remark" varchar(500) DEFAULT NULL,
|
||||||
|
"create_by" varchar(64) DEFAULT '',
|
||||||
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"update_by" varchar(64) DEFAULT '',
|
||||||
|
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '参数配置表';
|
||||||
|
|
||||||
|
-- sys 开头的 DB
|
||||||
|
|
||||||
|
CREATE TABLE "sys_dept" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"name" varchar(30) NOT NULL DEFAULT '',
|
||||||
|
"parent_id" bigint NOT NULL DEFAULT '0',
|
||||||
|
"sort" int NOT NULL DEFAULT '0',
|
||||||
|
"leader" varchar(20) DEFAULT NULL,
|
||||||
|
"phone" varchar(11) DEFAULT NULL,
|
||||||
|
"email" varchar(50) DEFAULT NULL,
|
||||||
|
"status" tinyint NOT NULL,
|
||||||
|
"create_by" varchar(64) DEFAULT '',
|
||||||
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"update_by" varchar(64) DEFAULT '',
|
||||||
|
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '部门表';
|
||||||
|
|
||||||
|
CREATE TABLE "sys_dict_data" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"sort" int NOT NULL DEFAULT '0',
|
||||||
|
"label" varchar(100) NOT NULL DEFAULT '',
|
||||||
|
"value" varchar(100) NOT NULL DEFAULT '',
|
||||||
|
"dict_type" varchar(100) NOT NULL DEFAULT '',
|
||||||
|
"status" tinyint NOT NULL DEFAULT '0',
|
||||||
|
"remark" varchar(500) DEFAULT NULL,
|
||||||
|
"create_by" varchar(64) DEFAULT '',
|
||||||
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"update_by" varchar(64) DEFAULT '',
|
||||||
|
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '字典数据表';
|
||||||
|
|
||||||
|
CREATE TABLE "sys_role" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"name" varchar(30) NOT NULL,
|
||||||
|
"code" varchar(100) NOT NULL,
|
||||||
|
"sort" int NOT NULL,
|
||||||
|
"data_scope" tinyint NOT NULL DEFAULT '1',
|
||||||
|
"data_scope_dept_ids" varchar(500) NOT NULL DEFAULT '',
|
||||||
|
"status" tinyint NOT NULL,
|
||||||
|
"type" tinyint NOT NULL,
|
||||||
|
"remark" varchar(500) DEFAULT NULL,
|
||||||
|
"create_by" varchar(64) DEFAULT '',
|
||||||
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"update_by" varchar(64) DEFAULT '',
|
||||||
|
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '角色信息表';
|
||||||
|
|
||||||
|
CREATE TABLE "sys_role_menu" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"role_id" bigint NOT NULL,
|
||||||
|
"menu_id" bigint NOT NULL,
|
||||||
|
"create_by" varchar(64) DEFAULT '',
|
||||||
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"update_by" varchar(64) DEFAULT '',
|
||||||
|
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '角色和菜单关联表';
|
||||||
|
|
||||||
|
CREATE TABLE "sys_menu" (
|
||||||
|
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
|
"name" varchar(50) NOT NULL,
|
||||||
|
"permission" varchar(100) NOT NULL DEFAULT '',
|
||||||
|
"menu_type" tinyint NOT NULL,
|
||||||
|
"sort" int NOT NULL DEFAULT '0',
|
||||||
|
"parent_id" bigint NOT NULL DEFAULT '0',
|
||||||
|
"path" varchar(200) DEFAULT '',
|
||||||
|
"icon" varchar(100) DEFAULT '#',
|
||||||
|
"component" varchar(255) DEFAULT NULL,
|
||||||
|
"status" tinyint NOT NULL DEFAULT '0',
|
||||||
|
"create_by" varchar(64) DEFAULT '',
|
||||||
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"update_by" varchar(64) DEFAULT '',
|
||||||
|
"update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||||
|
PRIMARY KEY ("id")
|
||||||
|
) COMMENT '菜单权限表';
|
||||||
Loading…
Reference in New Issue