1. 验证码的单元测试

2. 修复 h2 脚本的问题
This commit is contained in:
YunaiV 2021-03-13 19:34:26 +08:00
parent cee1aa3e60
commit 8af60fdaa6
13 changed files with 119 additions and 57 deletions

View File

@ -21,8 +21,8 @@ public class SysCaptchaController {
@Resource
private SysCaptchaService captchaService;
@ApiOperation("生成图片验证码")
@GetMapping("/get-image")
@ApiOperation("生成图片验证码")
public CommonResult<SysCaptchaImageRespVO> getCaptchaImage() {
return success(captchaService.getCaptchaImage());
}

View File

@ -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 芋道源码
*/

View File

@ -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 {
}
}

View File

@ -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<Object>) connection -> {
connection.flushDb();
return null;
});
}
}

View File

@ -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 {
/**

View File

@ -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;

View File

@ -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));
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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() {

View File

@ -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;

View File

@ -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 '',