mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 15:21:53 +08:00
1. 验证码的单元测试
2. 修复 h2 脚本的问题
This commit is contained in:
parent
cee1aa3e60
commit
8af60fdaa6
@ -21,8 +21,8 @@ public class SysCaptchaController {
|
||||
@Resource
|
||||
private SysCaptchaService captchaService;
|
||||
|
||||
@ApiOperation("生成图片验证码")
|
||||
@GetMapping("/get-image")
|
||||
@ApiOperation("生成图片验证码")
|
||||
public CommonResult<SysCaptchaImageRespVO> getCaptchaImage() {
|
||||
return success(captchaService.getCaptchaImage());
|
||||
}
|
||||
|
@ -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 芋道源码
|
||||
*/
|
||||
|
32
src/test/java/cn/iocoder/dashboard/BaseRedisUnitTest.java
Normal file
32
src/test/java/cn/iocoder/dashboard/BaseRedisUnitTest.java
Normal 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 {
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
|
@ -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 '',
|
||||
|
Loading…
Reference in New Issue
Block a user