Commit 7bb2dd11677af52cdde7ba4a9a5b36b0ce5e5b9b
1 parent
2f0d7e8a
引入 redis mock 服务器
Showing
16 changed files
with
101 additions
and
46 deletions
pom.xml
| ... | ... | @@ -183,6 +183,13 @@ |
| 183 | 183 | <scope>test</scope> |
| 184 | 184 | </dependency> |
| 185 | 185 | |
| 186 | + <dependency> | |
| 187 | + <groupId>com.github.fppt</groupId> <!-- 单元测试,我们采用内嵌的 Redis 数据库 --> | |
| 188 | + <artifactId>jedis-mock</artifactId> | |
| 189 | + <version>0.1.16</version> | |
| 190 | + <scope>test</scope> | |
| 191 | + </dependency> | |
| 192 | + | |
| 186 | 193 | <!-- 工具类相关 --> |
| 187 | 194 | <dependency> |
| 188 | 195 | <groupId>org.projectlombok</groupId> | ... | ... |
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; |
| 13 | 13 | import io.swagger.annotations.Api; |
| 14 | 14 | import io.swagger.annotations.ApiImplicitParam; |
| 15 | 15 | import io.swagger.annotations.ApiOperation; |
| 16 | -import org.springframework.beans.factory.annotation.Value; | |
| 17 | 16 | import org.springframework.validation.annotation.Validated; |
| 18 | 17 | import org.springframework.web.bind.annotation.*; |
| 19 | 18 | |
| ... | ... | @@ -30,19 +29,6 @@ import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CON |
| 30 | 29 | @RequestMapping("/infra/config") |
| 31 | 30 | public class InfConfigController { |
| 32 | 31 | |
| 33 | - @Value("${demo.test}") | |
| 34 | - private String demo; | |
| 35 | - | |
| 36 | - @GetMapping("/demo") | |
| 37 | - public String demo() { | |
| 38 | - return demo; | |
| 39 | - } | |
| 40 | - | |
| 41 | - @PostMapping("/demo") | |
| 42 | - public void setDemo() { | |
| 43 | - | |
| 44 | - } | |
| 45 | - | |
| 46 | 32 | @Resource |
| 47 | 33 | private InfConfigService configService; |
| 48 | 34 | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenColumnMapper.java renamed to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenColumnMapper.java
| 1 | -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; | |
| 1 | +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenTableMapper.java renamed to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolCodegenTableMapper.java
src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java renamed to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaColumnMapper.java
| 1 | -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; | |
| 1 | +package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java renamed to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolSchemaTableMapper.java
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 |
| 10 | 10 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; |
| 11 | 11 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; |
| 12 | 12 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; |
| 13 | -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenColumnMapper; | |
| 14 | -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper; | |
| 15 | -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaColumnMapper; | |
| 16 | -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaTableMapper; | |
| 13 | +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenColumnMapper; | |
| 14 | +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenTableMapper; | |
| 15 | +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolSchemaColumnMapper; | |
| 16 | +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolSchemaTableMapper; | |
| 17 | 17 | import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenImportTypeEnum; |
| 18 | 18 | import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; |
| 19 | 19 | import cn.iocoder.dashboard.util.collection.CollectionUtils; | ... | ... |
src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard; | |
| 2 | + | |
| 3 | +import org.junit.jupiter.api.AfterEach; | |
| 4 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 5 | +import org.springframework.data.redis.core.RedisCallback; | |
| 6 | +import org.springframework.data.redis.core.StringRedisTemplate; | |
| 7 | +import org.springframework.test.context.ActiveProfiles; | |
| 8 | +import org.springframework.test.context.jdbc.Sql; | |
| 9 | + | |
| 10 | +import javax.annotation.Resource; | |
| 11 | + | |
| 12 | +@SpringBootTest | |
| 13 | +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 | |
| 14 | +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB | |
| 15 | +public class BaseSpringBootUnitTest { | |
| 16 | + | |
| 17 | + @Resource | |
| 18 | + private StringRedisTemplate stringRedisTemplate; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * 每个单元测试结束后,清理 Redis | |
| 22 | + */ | |
| 23 | + @AfterEach | |
| 24 | + public void cleanRedis() { | |
| 25 | + stringRedisTemplate.execute((RedisCallback<Object>) connection -> { | |
| 26 | + connection.flushDb(); | |
| 27 | + return null; | |
| 28 | + }); | |
| 29 | + } | |
| 30 | + | |
| 31 | +} | ... | ... |
src/test/java/cn/iocoder/dashboard/config/QuartzTestConfiguration.java
| 1 | 1 | package cn.iocoder.dashboard.config; |
| 2 | 2 | |
| 3 | +import io.reactivex.rxjava3.core.Scheduler; | |
| 4 | +import org.mockito.Mockito; | |
| 5 | +import org.springframework.context.annotation.Bean; | |
| 3 | 6 | import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.context.annotation.Lazy; | |
| 4 | 8 | |
| 5 | 9 | @Configuration |
| 10 | +@Lazy(false) | |
| 6 | 11 | public class QuartzTestConfiguration { |
| 7 | 12 | |
| 8 | - // TODO 芋艿:暂时不用 Scheduler | |
| 9 | -// @Bean | |
| 10 | -// public Scheduler scheduler() { | |
| 11 | -// return Mockito.mock(Scheduler.class); | |
| 12 | -// } | |
| 13 | + @Bean | |
| 14 | + public Scheduler scheduler() { | |
| 15 | + return Mockito.mock(Scheduler.class); | |
| 16 | + } | |
| 13 | 17 | |
| 14 | 18 | } | ... | ... |
src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.config; | |
| 2 | + | |
| 3 | +import com.github.fppt.jedismock.RedisServer; | |
| 4 | +import org.redisson.spring.starter.RedissonAutoConfiguration; | |
| 5 | +import org.springframework.boot.autoconfigure.AutoConfigureBefore; | |
| 6 | +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; | |
| 7 | +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; | |
| 8 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 9 | +import org.springframework.context.annotation.Bean; | |
| 10 | +import org.springframework.context.annotation.Configuration; | |
| 11 | +import org.springframework.context.annotation.Lazy; | |
| 12 | + | |
| 13 | +import java.io.IOException; | |
| 14 | + | |
| 15 | +@Configuration | |
| 16 | +@Lazy(false) // 禁用懒加载,因为需要保证 Redis Server 必须先启动 | |
| 17 | +@EnableConfigurationProperties(RedisProperties.class) | |
| 18 | +@AutoConfigureBefore({RedisAutoConfiguration.class, RedissonAutoConfiguration.class}) // 在 Redis 自动配置前,进行初始化 | |
| 19 | +public class RedisTestConfiguration { | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * 创建模拟的 Redis Server 服务器 | |
| 23 | + */ | |
| 24 | + @Bean(initMethod = "start", destroyMethod = "stop") | |
| 25 | + public RedisServer redisServer(RedisProperties properties) throws IOException { | |
| 26 | + return new RedisServer(properties.getPort()); | |
| 27 | + } | |
| 28 | + | |
| 29 | +} | ... | ... |
src/test/java/cn/iocoder/dashboard/framework/quartz/core/scheduler/SchedulerManagerTest.java
| 1 | 1 | package cn.iocoder.dashboard.framework.quartz.core.scheduler; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | +import cn.iocoder.dashboard.BaseSpringBootUnitTest; | |
| 4 | 5 | import cn.iocoder.dashboard.modules.system.job.auth.SysUserSessionTimeoutJob; |
| 5 | 6 | import org.junit.jupiter.api.Test; |
| 6 | 7 | import org.quartz.SchedulerException; |
| 7 | -import org.springframework.boot.test.context.SpringBootTest; | |
| 8 | 8 | |
| 9 | 9 | import javax.annotation.Resource; |
| 10 | 10 | |
| 11 | -@SpringBootTest | |
| 12 | -class SchedulerManagerTest { | |
| 11 | +class SchedulerManagerTest extends BaseSpringBootUnitTest { | |
| 13 | 12 | |
| 14 | 13 | @Resource |
| 15 | 14 | private SchedulerManager schedulerManager; | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java
| 1 | 1 | package cn.iocoder.dashboard.modules.infra.service.config; |
| 2 | 2 | |
| 3 | +import cn.iocoder.dashboard.BaseSpringBootUnitTest; | |
| 3 | 4 | import cn.iocoder.dashboard.common.exception.ServiceException; |
| 4 | 5 | import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO; |
| 5 | 6 | import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO; |
| ... | ... | @@ -9,9 +10,7 @@ import cn.iocoder.dashboard.modules.infra.mq.producer.config.InfConfigProducer; |
| 9 | 10 | import cn.iocoder.dashboard.modules.infra.service.config.impl.InfConfigServiceImpl; |
| 10 | 11 | import cn.iocoder.dashboard.util.AssertUtils; |
| 11 | 12 | import org.junit.jupiter.api.Test; |
| 12 | -import org.springframework.boot.test.context.SpringBootTest; | |
| 13 | 13 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | -import org.springframework.test.context.ActiveProfiles; | |
| 15 | 14 | import org.springframework.test.context.jdbc.Sql; |
| 16 | 15 | |
| 17 | 16 | import javax.annotation.Resource; |
| ... | ... | @@ -21,10 +20,7 @@ import static org.junit.jupiter.api.Assertions.*; |
| 21 | 20 | import static org.mockito.Mockito.times; |
| 22 | 21 | import static org.mockito.Mockito.verify; |
| 23 | 22 | |
| 24 | -@SpringBootTest | |
| 25 | -@ActiveProfiles("unit-test") | |
| 26 | -@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) | |
| 27 | -public class InfConfigServiceImplTest { | |
| 23 | +public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | |
| 28 | 24 | |
| 29 | 25 | @Resource |
| 30 | 26 | private InfConfigServiceImpl configService; | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java renamed to src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java
src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java renamed to src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java
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; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; |
| 4 | 4 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; |
| 5 | -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenColumnMapper; | |
| 6 | -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper; | |
| 5 | +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenColumnMapper; | |
| 6 | +import cn.iocoder.dashboard.modules.tool.dal.mysql.codegen.ToolCodegenTableMapper; | |
| 7 | 7 | import org.junit.jupiter.api.Test; |
| 8 | 8 | import org.springframework.boot.test.context.SpringBootTest; |
| 9 | 9 | ... | ... |
src/test/resources/application-unit-test.yaml
| 1 | 1 | spring: |
| 2 | 2 | main: |
| 3 | - lazy-initialization: true | |
| 3 | + lazy-initialization: true # 开启懒加载,加快速度 | |
| 4 | + banner-mode: off # 单元测试,禁用 Banner | |
| 4 | 5 | |
| 5 | 6 | # 去除的自动配置项 |
| 6 | 7 | autoconfigure: |
| ... | ... | @@ -10,6 +11,11 @@ spring: |
| 10 | 11 | - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 单元测试,禁用 Quartz |
| 11 | 12 | - com.baomidou.lock.spring.boot.autoconfigure.LockAutoConfiguration # 单元测试,禁用 Lock4j 分布式锁 |
| 12 | 13 | |
| 14 | +# Swagger 接口文档的自动配置(单元测试,禁用 Swagger) | |
| 15 | +springfox: | |
| 16 | + documentation: | |
| 17 | + auto-startup: false | |
| 18 | + | |
| 13 | 19 | --- #################### 数据库相关配置 #################### |
| 14 | 20 | |
| 15 | 21 | spring: |
| ... | ... | @@ -25,7 +31,7 @@ spring: |
| 25 | 31 | # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 |
| 26 | 32 | redis: |
| 27 | 33 | host: 127.0.0.1 # 地址 |
| 28 | - port: 6379 # 端口 | |
| 34 | + port: 36379 # 端口(单元测试,使用 363979 端口) | |
| 29 | 35 | database: 0 # 数据库索引 |
| 30 | 36 | |
| 31 | 37 | --- #################### 定时任务相关配置 #################### |
| ... | ... | @@ -70,10 +76,7 @@ spring: |
| 70 | 76 | # Spring Boot Admin Server 服务端的相关配置 |
| 71 | 77 | context-path: /admin # 配置 Spring |
| 72 | 78 | |
| 73 | -# 日志文件配置 | |
| 74 | -logging: | |
| 75 | - file: | |
| 76 | - path: ${user.home}/logs/ # 日志文件的路径 | |
| 79 | +# 日志文件配置(不需要配置) | |
| 77 | 80 | |
| 78 | 81 | --- #################### 芋道相关配置 #################### |
| 79 | 82 | ... | ... |