Browse Source

解决 redis 和 mysql 在单元测试会报错的问题

YunaiV 4 years ago
parent
commit
3320f42bd7

+ 3 - 3
src/test/java/cn/iocoder/dashboard/config/QuartzTestConfiguration.java

@@ -1,7 +1,7 @@
 package cn.iocoder.dashboard.config;
 
-import io.reactivex.rxjava3.core.Scheduler;
 import org.mockito.Mockito;
+import org.quartz.impl.StdScheduler;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Lazy;
@@ -11,8 +11,8 @@ import org.springframework.context.annotation.Lazy;
 public class QuartzTestConfiguration {
 
     @Bean
-    public Scheduler scheduler() {
-        return Mockito.mock(Scheduler.class);
+    public StdScheduler scheduler() {
+        return Mockito.mock(StdScheduler.class);
     }
 
 }

+ 14 - 2
src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java

@@ -18,12 +18,24 @@ import java.io.IOException;
 @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());
+//    }
+
     /**
      * 创建模拟的 Redis Server 服务器
      */
-    @Bean(initMethod = "start", destroyMethod = "stop")
+    @Bean(destroyMethod = "stop")
     public RedisServer redisServer(RedisProperties properties) throws IOException {
-        return new RedisServer(properties.getPort());
+        RedisServer redisServer = new RedisServer(properties.getPort());
+        try {
+            redisServer.start();
+        } catch (Exception ignore) {}
+        return redisServer;
     }
 
 }

+ 6 - 6
src/test/resources/sql/create_tables.sql

@@ -1,6 +1,6 @@
 -- inf 开头的 DB
 
-CREATE TABLE "inf_config" (
+CREATE TABLE IF NOT EXISTS "inf_config" (
     "id" int NOT NULL GENERATED BY DEFAULT AS IDENTITY,
     "group" varchar(50) NOT NULL,
     "type" tinyint NOT NULL,
@@ -19,7 +19,7 @@ CREATE TABLE "inf_config" (
 
 -- sys 开头的 DB
 
-CREATE TABLE "sys_dept" (
+CREATE TABLE IF NOT EXISTS "sys_dept" (
     "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
     "name" varchar(30) NOT NULL DEFAULT '',
     "parent_id" bigint NOT NULL DEFAULT '0',
@@ -36,7 +36,7 @@ CREATE TABLE "sys_dept" (
     PRIMARY KEY ("id")
 ) COMMENT '部门表';
 
-CREATE TABLE "sys_dict_data" (
+CREATE TABLE IF NOT EXISTS "sys_dict_data" (
     "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
     "sort" int NOT NULL DEFAULT '0',
     "label" varchar(100) NOT NULL DEFAULT '',
@@ -52,7 +52,7 @@ CREATE TABLE "sys_dict_data" (
     PRIMARY KEY ("id")
 ) COMMENT '字典数据表';
 
-CREATE TABLE "sys_role" (
+CREATE TABLE IF NOT EXISTS "sys_role" (
     "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
     "name" varchar(30) NOT NULL,
     "code" varchar(100) NOT NULL,
@@ -70,7 +70,7 @@ CREATE TABLE "sys_role" (
     PRIMARY KEY ("id")
 ) COMMENT '角色信息表';
 
-CREATE TABLE "sys_role_menu" (
+CREATE TABLE IF NOT EXISTS "sys_role_menu" (
     "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
     "role_id" bigint NOT NULL,
     "menu_id" bigint NOT NULL,
@@ -82,7 +82,7 @@ CREATE TABLE "sys_role_menu" (
     PRIMARY KEY ("id")
 ) COMMENT '角色和菜单关联表';
 
-CREATE TABLE "sys_menu" (
+CREATE TABLE IF NOT EXISTS "sys_menu" (
     "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
     "name" varchar(50) NOT NULL,
     "permission" varchar(100) NOT NULL DEFAULT '',