mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-31 19:34:06 +08:00
多模块重构 11:修改代码生成器的实现
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.module.tool.service;
|
@@ -0,0 +1,187 @@
|
||||
package cn.iocoder.yudao.module.tool.service.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import cn.iocoder.yudao.module.tool.test.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.tool.controller.app.test.vo.*;
|
||||
import cn.iocoder.yudao.module.tool.dal.dataobject.test.TestDemoDO;
|
||||
import cn.iocoder.yudao.module.tool.dal.mysql.test.TestDemoMapper;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.*;
|
||||
import static cn.iocoder.yudao.module.tool.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link TestDemoServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(TestDemoServiceImpl.class)
|
||||
public class TestDemoServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private TestDemoServiceImpl testDemoService;
|
||||
|
||||
@Resource
|
||||
private TestDemoMapper testDemoMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateTestDemo_success() {
|
||||
// 准备参数
|
||||
AppTestDemoCreateReqVO reqVO = randomPojo(AppTestDemoCreateReqVO.class);
|
||||
|
||||
// 调用
|
||||
Long testDemoId = testDemoService.createTestDemo(reqVO);
|
||||
// 断言
|
||||
assertNotNull(testDemoId);
|
||||
// 校验记录的属性是否正确
|
||||
TestDemoDO testDemo = testDemoMapper.selectById(testDemoId);
|
||||
assertPojoEquals(reqVO, testDemo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateTestDemo_success() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class);
|
||||
testDemoMapper.insert(dbTestDemo);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
AppTestDemoUpdateReqVO reqVO = randomPojo(AppTestDemoUpdateReqVO.class, o -> {
|
||||
o.setId(dbTestDemo.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
testDemoService.updateTestDemo(reqVO);
|
||||
// 校验是否更新正确
|
||||
TestDemoDO testDemo = testDemoMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, testDemo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateTestDemo_notExists() {
|
||||
// 准备参数
|
||||
AppTestDemoUpdateReqVO reqVO = randomPojo(AppTestDemoUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> testDemoService.updateTestDemo(reqVO), TEST_DEMO_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteTestDemo_success() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class);
|
||||
testDemoMapper.insert(dbTestDemo);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbTestDemo.getId();
|
||||
|
||||
// 调用
|
||||
testDemoService.deleteTestDemo(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(testDemoMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteTestDemo_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> testDemoService.deleteTestDemo(id), TEST_DEMO_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test // TODO 请修改 null 为需要的值
|
||||
public void testGetTestDemoPage() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class, o -> { // 等会查询到
|
||||
o.setName(null);
|
||||
o.setStatus(null);
|
||||
o.setType(null);
|
||||
o.setCategory(null);
|
||||
o.setRemark(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
testDemoMapper.insert(dbTestDemo);
|
||||
// 测试 name 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setName(null)));
|
||||
// 测试 status 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setStatus(null)));
|
||||
// 测试 type 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setType(null)));
|
||||
// 测试 category 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCategory(null)));
|
||||
// 测试 remark 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setRemark(null)));
|
||||
// 测试 createTime 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
AppTestDemoPageReqVO reqVO = new AppTestDemoPageReqVO();
|
||||
reqVO.setName(null);
|
||||
reqVO.setStatus(null);
|
||||
reqVO.setType(null);
|
||||
reqVO.setCategory(null);
|
||||
reqVO.setRemark(null);
|
||||
reqVO.setBeginCreateTime(null);
|
||||
reqVO.setEndCreateTime(null);
|
||||
|
||||
// 调用
|
||||
PageResult<TestDemoDO> pageResult = testDemoService.getTestDemoPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbTestDemo, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test // TODO 请修改 null 为需要的值
|
||||
public void testGetTestDemoList() {
|
||||
// mock 数据
|
||||
TestDemoDO dbTestDemo = randomPojo(TestDemoDO.class, o -> { // 等会查询到
|
||||
o.setName(null);
|
||||
o.setStatus(null);
|
||||
o.setType(null);
|
||||
o.setCategory(null);
|
||||
o.setRemark(null);
|
||||
o.setCreateTime(null);
|
||||
});
|
||||
testDemoMapper.insert(dbTestDemo);
|
||||
// 测试 name 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setName(null)));
|
||||
// 测试 status 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setStatus(null)));
|
||||
// 测试 type 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setType(null)));
|
||||
// 测试 category 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCategory(null)));
|
||||
// 测试 remark 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setRemark(null)));
|
||||
// 测试 createTime 不匹配
|
||||
testDemoMapper.insert(cloneIgnoreId(dbTestDemo, o -> o.setCreateTime(null)));
|
||||
// 准备参数
|
||||
AppTestDemoExportReqVO reqVO = new AppTestDemoExportReqVO();
|
||||
reqVO.setName(null);
|
||||
reqVO.setStatus(null);
|
||||
reqVO.setType(null);
|
||||
reqVO.setCategory(null);
|
||||
reqVO.setRemark(null);
|
||||
reqVO.setBeginCreateTime(null);
|
||||
reqVO.setEndCreateTime(null);
|
||||
|
||||
// 调用
|
||||
List<TestDemoDO> list = testDemoService.getTestDemoList(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbTestDemo, list.get(0));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.yudao.module.tool.test;
|
||||
|
||||
import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration;
|
||||
import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
/**
|
||||
* 依赖内存 DB 的单元测试
|
||||
*
|
||||
* 注意,Service 层同样适用。对于 Service 层的单元测试,我们针对自己模块的 Mapper 走的是 H2 内存数据库,针对别的模块的 Service 走的是 Mock 方法
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class)
|
||||
@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件
|
||||
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB
|
||||
public class BaseDbUnitTest {
|
||||
|
||||
@Import({
|
||||
// DB 配置类
|
||||
YudaoDataSourceAutoConfiguration.class, // 自己的 DB 配置类
|
||||
DataSourceAutoConfiguration.class, // Spring DB 自动配置类
|
||||
DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类
|
||||
DruidDataSourceAutoConfigure.class, // Druid 自动配置类
|
||||
// MyBatis 配置类
|
||||
YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类
|
||||
MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类
|
||||
})
|
||||
public static class Application {
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
spring:
|
||||
main:
|
||||
lazy-initialization: true # 开启懒加载,加快速度
|
||||
banner-mode: off # 单元测试,禁用 Banner
|
||||
|
||||
--- #################### 数据库相关配置 ####################
|
||||
|
||||
spring:
|
||||
# 数据源配置项
|
||||
datasource:
|
||||
name: ruoyi-vue-pro
|
||||
url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
schema: classpath:sql/create_tables.sql # MySQL 转 H2 的语句,使用 https://www.jooq.org/translate/ 工具
|
||||
druid:
|
||||
async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度
|
||||
initial-size: 1 # 单元测试,配置为 1,提升启动速度
|
||||
|
||||
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
|
||||
redis:
|
||||
host: 127.0.0.1 # 地址
|
||||
port: 16379 # 端口(单元测试,使用 16379 端口)
|
||||
database: 0 # 数据库索引
|
||||
|
||||
mybatis:
|
||||
lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试
|
||||
|
||||
--- #################### 定时任务相关配置 ####################
|
||||
|
||||
--- #################### 配置中心相关配置 ####################
|
||||
|
||||
--- #################### 服务保障相关配置 ####################
|
||||
|
||||
# Lock4j 配置项(单元测试,禁用 Lock4j)
|
||||
|
||||
# Resilience4j 配置项
|
||||
|
||||
--- #################### 监控相关配置 ####################
|
||||
|
||||
--- #################### 芋道相关配置 ####################
|
||||
|
||||
# 芋道配置项,设置当前项目所有自定义的配置
|
||||
yudao:
|
||||
info:
|
||||
base-package: cn.iocoder.yudao.module
|
@@ -0,0 +1,4 @@
|
||||
<configuration>
|
||||
<!-- 引用 Spring Boot 的 logback 基础配置 -->
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
|
||||
</configuration>
|
Reference in New Issue
Block a user