From 8af60fdaa6b6c7dd4d9e73c18aedd6ee17a2f9e0 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 13 Mar 2021 19:34:26 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E9=AA=8C=E8=AF=81=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=202.=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=20h2=20=E8=84=9A=E6=9C=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/SysCaptchaController.java | 2 +- .../dashboard/BaseDbAndRedisUnitTest.java | 4 +- .../iocoder/dashboard/BaseRedisUnitTest.java | 32 +++++++++ .../dashboard/BaseSpringBootUnitTest.java | 32 --------- .../config/RedisTestConfiguration.java | 6 +- .../core/scheduler/SchedulerManagerTest.java | 4 +- .../service/common/SysCaptchaServiceTest.java | 66 +++++++++++++++++++ ...ToolInformationSchemaColumnMapperTest.java | 4 +- .../ToolInformationSchemaTableMapperTest.java | 4 +- .../codegen/impl/ToolCodegenEngineTest.java | 5 +- .../impl/ToolCodegenSQLParserTest.java | 4 +- .../impl/ToolCodegenServiceImplTest.java | 5 +- src/test/resources/sql/create_tables.sql | 8 +-- 13 files changed, 119 insertions(+), 57 deletions(-) create mode 100644 src/test/java/cn/iocoder/dashboard/BaseRedisUnitTest.java delete mode 100644 src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java create mode 100644 src/test/java/cn/iocoder/dashboard/modules/system/service/common/SysCaptchaServiceTest.java diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/common/SysCaptchaController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/common/SysCaptchaController.java index 13e84dc4e..487389331 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/common/SysCaptchaController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/common/SysCaptchaController.java @@ -21,8 +21,8 @@ public class SysCaptchaController { @Resource private SysCaptchaService captchaService; - @ApiOperation("生成图片验证码") @GetMapping("/get-image") + @ApiOperation("生成图片验证码") public CommonResult getCaptchaImage() { return success(captchaService.getCaptchaImage()); } diff --git a/src/test/java/cn/iocoder/dashboard/BaseDbAndRedisUnitTest.java b/src/test/java/cn/iocoder/dashboard/BaseDbAndRedisUnitTest.java index 59bacb052..43d28da9f 100644 --- a/src/test/java/cn/iocoder/dashboard/BaseDbAndRedisUnitTest.java +++ b/src/test/java/cn/iocoder/dashboard/BaseDbAndRedisUnitTest.java @@ -15,9 +15,9 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.jdbc.Sql; /** - * 依赖内存 DB 的单元测试 + * 依赖内存 DB + Redis 的单元测试 * - * 注意,Service 层同样适用。对于 Service 层的单元测试,我们针对自己模块的 Mapper 走的是 H2 内存数据库,针对别的模块的 Service 走的是 Mock 方法 + * 相比 {@link BaseDbUnitTest} 来说,额外增加了内存 Redis * * @author 芋道源码 */ diff --git a/src/test/java/cn/iocoder/dashboard/BaseRedisUnitTest.java b/src/test/java/cn/iocoder/dashboard/BaseRedisUnitTest.java new file mode 100644 index 000000000..a806e4670 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/BaseRedisUnitTest.java @@ -0,0 +1,32 @@ +package cn.iocoder.dashboard; + +import cn.iocoder.dashboard.config.RedisTestConfiguration; +import cn.iocoder.dashboard.framework.redis.config.RedisConfig; +import org.redisson.spring.starter.RedissonAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +/** + * 依赖内存 Redis 的单元测试 + * + * 相比 {@link BaseDbUnitTest} 来说,从内存 DB 改成了内存 Redis + * + * @author 芋道源码 + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseRedisUnitTest.Application.class) +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 +public class BaseRedisUnitTest { + + @Import({ + // Redis 配置类 + RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer + RedisAutoConfiguration.class, // Spring Redis 自动配置类 + RedisConfig.class, // 自己的 Redis 配置类 + RedissonAutoConfiguration.class, // Redisson 自动高配置类 + }) + public static class Application { + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java b/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java deleted file mode 100644 index fe615dcaf..000000000 --- a/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java +++ /dev/null @@ -1,32 +0,0 @@ -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(webEnvironment = SpringBootTest.WebEnvironment.NONE) -@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 -@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB -@Deprecated -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/RedisTestConfiguration.java b/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java index c93d766a4..29539dbad 100644 --- a/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java +++ b/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java @@ -1,19 +1,17 @@ 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(proxyBeanMethods = false) +@Lazy(false) // 禁止延迟加载 @EnableConfigurationProperties(RedisProperties.class) -@AutoConfigureBefore({RedisAutoConfiguration.class, RedissonAutoConfiguration.class}) // 在 Redis 自动配置前,进行初始化 public class RedisTestConfiguration { /** 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 a9b050153..a4dd5cbe0 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,14 +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.BaseDbUnitTest; import cn.iocoder.dashboard.modules.system.job.auth.SysUserSessionTimeoutJob; import org.junit.jupiter.api.Test; import org.quartz.SchedulerException; import javax.annotation.Resource; -class SchedulerManagerTest extends BaseSpringBootUnitTest { +class SchedulerManagerTest extends BaseDbUnitTest { @Resource private SchedulerManager schedulerManager; diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/common/SysCaptchaServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/common/SysCaptchaServiceTest.java new file mode 100644 index 000000000..ed127442d --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/common/SysCaptchaServiceTest.java @@ -0,0 +1,66 @@ +package cn.iocoder.dashboard.modules.system.service.common; + +import cn.iocoder.dashboard.BaseRedisUnitTest; +import cn.iocoder.dashboard.framework.captcha.config.CaptchaProperties; +import cn.iocoder.dashboard.modules.system.controller.common.vo.SysCaptchaImageRespVO; +import cn.iocoder.dashboard.modules.system.dal.redis.common.SysCaptchaRedisDAO; +import cn.iocoder.dashboard.modules.system.service.common.impl.SysCaptchaServiceImpl; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.Import; + +import javax.annotation.Resource; + +import static cn.iocoder.dashboard.util.RandomUtils.randomString; +import static org.junit.jupiter.api.Assertions.*; + +@Import({SysCaptchaServiceImpl.class, CaptchaProperties.class, SysCaptchaRedisDAO.class}) +public class SysCaptchaServiceTest extends BaseRedisUnitTest { + + @Resource + private SysCaptchaServiceImpl captchaService; + + @Resource + private SysCaptchaRedisDAO captchaRedisDAO; + @Resource + private CaptchaProperties captchaProperties; + + @Test + public void testGetCaptchaImage() { + // 调用 + SysCaptchaImageRespVO respVO = captchaService.getCaptchaImage(); + // 断言 + assertNotNull(respVO.getUuid()); + assertNotNull(respVO.getImg()); + String captchaCode = captchaRedisDAO.get(respVO.getUuid()); + assertNotNull(captchaCode); + } + + @Test + public void testGetCaptchaCode() { + // 准备参数 + String uuid = randomString(); + String code = randomString(); + // mock 数据 + captchaRedisDAO.set(uuid, code, captchaProperties.getTimeout()); + + // 调用 + String resultCode = captchaService.getCaptchaCode(uuid); + // 断言 + assertEquals(code, resultCode); + } + + @Test + public void testDeleteCaptchaCode() { + // 准备参数 + String uuid = randomString(); + String code = randomString(); + // mock 数据 + captchaRedisDAO.set(uuid, code, captchaProperties.getTimeout()); + + // 调用 + captchaService.deleteCaptchaCode(uuid); + // 断言 + assertNull(captchaRedisDAO.get(uuid)); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java index 1b7e1f283..dfb64a8ea 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaColumnMapperTest.java @@ -1,6 +1,6 @@ package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; -import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.BaseDbUnitTest; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; import org.junit.jupiter.api.Test; @@ -9,7 +9,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertTrue; -public class ToolInformationSchemaColumnMapperTest extends BaseSpringBootUnitTest { +public class ToolInformationSchemaColumnMapperTest extends BaseDbUnitTest { @Resource private ToolSchemaColumnMapper toolInformationSchemaColumnMapper; diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java index ff488972c..67e8f7066 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/codegen/ToolInformationSchemaTableMapperTest.java @@ -1,6 +1,6 @@ package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen; -import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.BaseDbUnitTest; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import org.junit.jupiter.api.Test; @@ -9,7 +9,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertTrue; -class ToolInformationSchemaTableMapperTest extends BaseSpringBootUnitTest { +class ToolInformationSchemaTableMapperTest extends BaseDbUnitTest { @Resource private ToolSchemaTableMapper toolInformationSchemaTableMapper; 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 e415b4f84..94ade49f4 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 @@ -1,18 +1,17 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; -import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.BaseDbUnitTest; 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.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; import javax.annotation.Resource; import java.util.List; import java.util.Map; -public class ToolCodegenEngineTest extends BaseSpringBootUnitTest { +public class ToolCodegenEngineTest extends BaseDbUnitTest { @Resource private ToolCodegenTableMapper codegenTableMapper; diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenSQLParserTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenSQLParserTest.java index 1c331dfdf..f47fce0c3 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenSQLParserTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenSQLParserTest.java @@ -1,9 +1,9 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; -import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.BaseDbUnitTest; import org.junit.jupiter.api.Test; -public class ToolCodegenSQLParserTest extends BaseSpringBootUnitTest { +public class ToolCodegenSQLParserTest extends BaseDbUnitTest { @Test public void testParse() { diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java index 603ae9a47..1198a52c7 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java @@ -1,12 +1,11 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; -import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.BaseDbUnitTest; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; -class ToolCodegenServiceImplTest extends BaseSpringBootUnitTest { +class ToolCodegenServiceImplTest extends BaseDbUnitTest { @Resource private ToolCodegenServiceImpl toolCodegenService; diff --git a/src/test/resources/sql/create_tables.sql b/src/test/resources/sql/create_tables.sql index 36c573dd9..4a9d218b9 100644 --- a/src/test/resources/sql/create_tables.sql +++ b/src/test/resources/sql/create_tables.sql @@ -113,7 +113,7 @@ CREATE TABLE IF NOT EXISTS "sys_menu" ( PRIMARY KEY ("id") ) COMMENT '菜单权限表'; -CREATE TABLE "sys_dict_type" ( +CREATE TABLE IF NOT EXISTS "sys_dict_type" ( "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" varchar(100) NOT NULL DEFAULT '', "type" varchar(100) NOT NULL DEFAULT '', @@ -127,7 +127,7 @@ CREATE TABLE "sys_dict_type" ( PRIMARY KEY ("id") ) COMMENT '字典类型表'; -CREATE TABLE `sys_user_session` ( +CREATE TABLE IF NOT EXISTS `sys_user_session` ( `id` varchar(32) NOT NULL, `user_id` bigint DEFAULT NULL, `username` varchar(50) NOT NULL DEFAULT '', @@ -191,7 +191,7 @@ CREATE TABLE IF NOT EXISTS `sys_login_log` ( ) COMMENT ='系统访问记录'; -CREATE TABLE `sys_operate_log` ( +CREATE TABLE IF NOT EXISTS `sys_operate_log` ( `id` bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, `trace_id` varchar(64) NOT NULL DEFAULT '', `user_id` bigint(20) NOT NULL, @@ -219,7 +219,7 @@ CREATE TABLE `sys_operate_log` ( PRIMARY KEY (`id`) ) COMMENT ='操作日志记录'; -create table "sys_user" ( +create table IF NOT EXISTS "sys_user" ( "id" bigint not null GENERATED BY DEFAULT AS IDENTITY, "username" varchar(30) not null, "password" varchar(100) not null default '',