From 7bb2dd11677af52cdde7ba4a9a5b36b0ce5e5b9b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 28 Feb 2021 23:38:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=20redis=20mock=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 +++++ .../config/InfConfigController.java | 14 --------- .../ToolCodegenColumnMapper.java | 2 +- .../ToolCodegenTableMapper.java | 2 +- .../ToolSchemaColumnMapper.java | 2 +- .../ToolSchemaTableMapper.java | 2 +- .../codegen/impl/ToolCodegenServiceImpl.java | 8 ++--- .../dashboard/BaseSpringBootUnitTest.java | 31 +++++++++++++++++++ .../config/QuartzTestConfiguration.java | 14 ++++++--- .../config/RedisTestConfiguration.java | 29 +++++++++++++++++ .../core/scheduler/SchedulerManagerTest.java | 5 ++- .../config/InfConfigServiceImplTest.java | 8 ++--- ...ToolInformationSchemaColumnMapperTest.java | 2 +- .../ToolInformationSchemaTableMapperTest.java | 2 +- .../codegen/impl/ToolCodegenEngineTest.java | 4 +-- src/test/resources/application-unit-test.yaml | 15 +++++---- 16 files changed, 101 insertions(+), 46 deletions(-) rename src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/{coegen => codegen}/ToolCodegenColumnMapper.java (92%) rename src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/{coegen => codegen}/ToolCodegenTableMapper.java (95%) rename src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/{coegen => codegen}/ToolSchemaColumnMapper.java (90%) rename src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/{coegen => codegen}/ToolSchemaTableMapper.java (95%) create mode 100644 src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java create mode 100644 src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java rename src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/{coegen => codegen}/ToolInformationSchemaColumnMapperTest.java (93%) rename src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/{coegen => codegen}/ToolInformationSchemaTableMapperTest.java (91%) diff --git a/pom.xml b/pom.xml index 3e5289c14..7ac446dba 100644 --- a/pom.xml +++ b/pom.xml @@ -183,6 +183,13 @@ test + + com.github.fppt + jedis-mock + 0.1.16 + test + + org.projectlombok diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java index 05549b3c2..fc39d9431 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/config/InfConfigController.java @@ -13,7 +13,6 @@ import cn.iocoder.dashboard.modules.infra.service.config.InfConfigService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; -import org.springframework.beans.factory.annotation.Value; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -30,19 +29,6 @@ import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CON @RequestMapping("/infra/config") public class InfConfigController { - @Value("${demo.test}") - private String demo; - - @GetMapping("/demo") - public String demo() { - return demo; - } - - @PostMapping("/demo") - public void setDemo() { - - } - @Resource private InfConfigService configService; diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenColumnMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenColumnMapper.java similarity index 92% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenColumnMapper.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenColumnMapper.java index 0539e13e6..e7fa43317 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenColumnMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenColumnMapper.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenTableMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenTableMapper.java similarity index 95% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenTableMapper.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenTableMapper.java index 8c47db62e..52ce76f2b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenTableMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenTableMapper.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaColumnMapper.java similarity index 90% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaColumnMapper.java index a891e75db..438168bc4 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaColumnMapper.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaTableMapper.java similarity index 95% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaTableMapper.java index 6b1dbf370..20a775272 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaTableMapper.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java index b93f628a4..6b2b5f9f7 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java @@ -10,10 +10,10 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenColumnMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaColumnMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaTableMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenColumnMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenTableMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolSchemaColumnMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolSchemaTableMapper; import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenImportTypeEnum; import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; import cn.iocoder.dashboard.util.collection.CollectionUtils; diff --git a/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java b/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java new file mode 100644 index 000000000..fa75d3533 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java @@ -0,0 +1,31 @@ +package cn.iocoder.dashboard; + +import org.junit.jupiter.api.AfterEach; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.RedisCallback; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; + +import javax.annotation.Resource; + +@SpringBootTest +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB +public class BaseSpringBootUnitTest { + + @Resource + private StringRedisTemplate stringRedisTemplate; + + /** + * 每个单元测试结束后,清理 Redis + */ + @AfterEach + public void cleanRedis() { + stringRedisTemplate.execute((RedisCallback) connection -> { + connection.flushDb(); + return null; + }); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/config/QuartzTestConfiguration.java b/src/test/java/cn/iocoder/dashboard/config/QuartzTestConfiguration.java index 87e7b89d9..24aa88169 100644 --- a/src/test/java/cn/iocoder/dashboard/config/QuartzTestConfiguration.java +++ b/src/test/java/cn/iocoder/dashboard/config/QuartzTestConfiguration.java @@ -1,14 +1,18 @@ package cn.iocoder.dashboard.config; +import io.reactivex.rxjava3.core.Scheduler; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; @Configuration +@Lazy(false) public class QuartzTestConfiguration { - // TODO 芋艿:暂时不用 Scheduler -// @Bean -// public Scheduler scheduler() { -// return Mockito.mock(Scheduler.class); -// } + @Bean + public Scheduler scheduler() { + return Mockito.mock(Scheduler.class); + } } diff --git a/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java b/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java new file mode 100644 index 000000000..f5df9ab3e --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java @@ -0,0 +1,29 @@ +package cn.iocoder.dashboard.config; + +import com.github.fppt.jedismock.RedisServer; +import org.redisson.spring.starter.RedissonAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; + +import java.io.IOException; + +@Configuration +@Lazy(false) // 禁用懒加载,因为需要保证 Redis Server 必须先启动 +@EnableConfigurationProperties(RedisProperties.class) +@AutoConfigureBefore({RedisAutoConfiguration.class, RedissonAutoConfiguration.class}) // 在 Redis 自动配置前,进行初始化 +public class RedisTestConfiguration { + + /** + * 创建模拟的 Redis Server 服务器 + */ + @Bean(initMethod = "start", destroyMethod = "stop") + public RedisServer redisServer(RedisProperties properties) throws IOException { + return new RedisServer(properties.getPort()); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java b/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java index c4d15df02..a9b050153 100644 --- a/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java +++ b/src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java @@ -1,15 +1,14 @@ package cn.iocoder.dashboard.framework.quartz.core.scheduler; import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.BaseSpringBootUnitTest; import cn.iocoder.dashboard.modules.system.job.auth.SysUserSessionTimeoutJob; import org.junit.jupiter.api.Test; import org.quartz.SchedulerException; -import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; -@SpringBootTest -class SchedulerManagerTest { +class SchedulerManagerTest extends BaseSpringBootUnitTest { @Resource private SchedulerManager schedulerManager; diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java index 1302e4cfc..3e4b93809 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java @@ -1,5 +1,6 @@ package cn.iocoder.dashboard.modules.infra.service.config; +import cn.iocoder.dashboard.BaseSpringBootUnitTest; 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; @@ -9,9 +10,7 @@ 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; @@ -21,10 +20,7 @@ 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 { +public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { @Resource private InfConfigServiceImpl configService; diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java similarity index 93% rename from src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java rename to src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java index 8c52de093..57503ff40 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; import org.junit.jupiter.api.Test; diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java similarity index 91% rename from src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java rename to src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java index 597a7c22b..6f48cc326 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import org.junit.jupiter.api.Test; diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java index e682f85a8..6231bc2eb 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java @@ -2,8 +2,8 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenColumnMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenColumnMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenTableMapper; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; diff --git a/src/test/resources/application-unit-test.yaml b/src/test/resources/application-unit-test.yaml index 2990ef681..9472c2fc2 100644 --- a/src/test/resources/application-unit-test.yaml +++ b/src/test/resources/application-unit-test.yaml @@ -1,6 +1,7 @@ spring: main: - lazy-initialization: true + lazy-initialization: true # 开启懒加载,加快速度 + banner-mode: off # 单元测试,禁用 Banner # 去除的自动配置项 autoconfigure: @@ -10,6 +11,11 @@ spring: - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 单元测试,禁用 Quartz - com.baomidou.lock.spring.boot.autoconfigure.LockAutoConfiguration # 单元测试,禁用 Lock4j 分布式锁 +# Swagger 接口文档的自动配置(单元测试,禁用 Swagger) +springfox: + documentation: + auto-startup: false + --- #################### 数据库相关配置 #################### spring: @@ -25,7 +31,7 @@ spring: # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 redis: host: 127.0.0.1 # 地址 - port: 6379 # 端口 + port: 36379 # 端口(单元测试,使用 363979 端口) database: 0 # 数据库索引 --- #################### 定时任务相关配置 #################### @@ -70,10 +76,7 @@ spring: # Spring Boot Admin Server 服务端的相关配置 context-path: /admin # 配置 Spring -# 日志文件配置 -logging: - file: - path: ${user.home}/logs/ # 日志文件的路径 +# 日志文件配置(不需要配置) --- #################### 芋道相关配置 ####################