Commit c58eb128965c9d529768a583630c8e1e8e757093
1 parent
81d89ba3
修复所有单元测试
Showing
31 changed files
with
219 additions
and
237 deletions
yudao-module-bpm/yudao-module-bpm-base/src/test/java/cn/iocoder/yudao/module/bpm/test/BaseDbUnitTest.java
| ... | ... | @@ -2,11 +2,11 @@ package cn.iocoder.yudao.module.bpm.test; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 5 | 6 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 6 | 7 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 7 | 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 8 | 9 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; |
| 9 | -import org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration; | |
| 10 | 10 | import org.springframework.boot.test.context.SpringBootTest; |
| 11 | 11 | import org.springframework.context.annotation.Import; |
| 12 | 12 | import org.springframework.test.context.ActiveProfiles; |
| ... | ... | @@ -21,7 +21,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 21 | 21 | */ |
| 22 | 22 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) |
| 23 | 23 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 24 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 25 | 24 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 26 | 25 | public class BaseDbUnitTest { |
| 27 | 26 | |
| ... | ... | @@ -31,7 +30,7 @@ public class BaseDbUnitTest { |
| 31 | 30 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 32 | 31 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 33 | 32 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 34 | - SqlInitializationAutoConfiguration.class, | |
| 33 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 35 | 34 | // MyBatis 配置类 |
| 36 | 35 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 37 | 36 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | ... | ... |
yudao-module-bpm/yudao-module-bpm-base/src/test/resources/application-unit-test.yaml
| ... | ... | @@ -16,6 +16,9 @@ spring: |
| 16 | 16 | druid: |
| 17 | 17 | async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 |
| 18 | 18 | initial-size: 1 # 单元测试,配置为 1,提升启动速度 |
| 19 | + sql: | |
| 20 | + init: | |
| 21 | + schema-locations: classpath:/sql/create_tables.sql | |
| 19 | 22 | |
| 20 | 23 | mybatis: |
| 21 | 24 | lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试 | ... | ... |
yudao-module-bpm/yudao-module-bpm-base/src/test/resources/sql/clean.sql
yudao-module-bpm/yudao-module-bpm-base/src/test/resources/sql/create_tables.sql
| 1 | --- bpm 开头的 DB | |
| 2 | 1 | CREATE TABLE IF NOT EXISTS "bpm_user_group" ( |
| 3 | 2 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, |
| 4 | 3 | "name" varchar(63) NOT NULL, |
| ... | ... | @@ -11,7 +10,7 @@ CREATE TABLE IF NOT EXISTS "bpm_user_group" ( |
| 11 | 10 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 12 | 11 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 13 | 12 | PRIMARY KEY ("id") |
| 14 | - ) COMMENT '用户组'; | |
| 13 | +) COMMENT '用户组'; | |
| 15 | 14 | |
| 16 | 15 | CREATE TABLE IF NOT EXISTS "bpm_form" ( |
| 17 | 16 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | ... | ... |
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/test/java/cn/iocoder/yudao/module/bpm/test/BaseDbUnitTest.java
| ... | ... | @@ -20,7 +20,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 20 | 20 | */ |
| 21 | 21 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) |
| 22 | 22 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 23 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 24 | 23 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 25 | 24 | public class BaseDbUnitTest { |
| 26 | 25 | ... | ... |
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/test/resources/application-unit-test.yaml
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/test/resources/sql/clean.sql
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/test/resources/sql/create_tables.sql
| 1 | -CREATE TABLE IF NOT EXISTS "bpm_form" ( | |
| 2 | - "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 3 | - "name" varchar(63) NOT NULL, | |
| 4 | - "status" tinyint NOT NULL, | |
| 5 | - "fields" varchar(255) NOT NULL, | |
| 6 | - "conf" varchar(255) NOT NULL, | |
| 7 | - "remark" varchar(255), | |
| 8 | - "creator" varchar(64) DEFAULT '', | |
| 9 | - "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 10 | - "updater" varchar(64) DEFAULT '', | |
| 11 | - "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 12 | - "deleted" bit NOT NULL DEFAULT FALSE, | |
| 13 | - PRIMARY KEY ("id") | |
| 14 | - ) COMMENT '动态表单'; | |
| 15 | - | |
| 16 | -CREATE TABLE IF NOT EXISTS "bpm_user_group" ( | |
| 17 | - "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 18 | - "name" varchar(63) NOT NULL, | |
| 19 | - "description" varchar(255) NOT NULL, | |
| 20 | - "status" tinyint NOT NULL, | |
| 21 | - "member_user_ids" varchar(255) NOT NULL, | |
| 22 | - "creator" varchar(64) DEFAULT '', | |
| 23 | - "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 24 | - "updater" varchar(64) DEFAULT '', | |
| 25 | - "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 26 | - "deleted" bit NOT NULL DEFAULT FALSE, | |
| 27 | - PRIMARY KEY ("id") | |
| 28 | - ) COMMENT '用户组'; |
yudao-module-infra/yudao-module-infra-impl/src/test/java/cn/iocoder/yudao/module/infra/test/BaseDbAndRedisUnitTest.java
| ... | ... | @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.infra.test; |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | 5 | import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; |
| 6 | +import cn.iocoder.yudao.framework.test.config.RedisTestConfiguration; | |
| 7 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 6 | 8 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 7 | 9 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 8 | 10 | import org.redisson.spring.starter.RedissonAutoConfiguration; |
| ... | ... | @@ -23,7 +25,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 23 | 25 | */ |
| 24 | 26 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisUnitTest.Application.class) |
| 25 | 27 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 26 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 27 | 28 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 28 | 29 | public class BaseDbAndRedisUnitTest { |
| 29 | 30 | |
| ... | ... | @@ -33,9 +34,11 @@ public class BaseDbAndRedisUnitTest { |
| 33 | 34 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 34 | 35 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 35 | 36 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 37 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 36 | 38 | // MyBatis 配置类 |
| 37 | 39 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 38 | 40 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 |
| 41 | + | |
| 39 | 42 | // Redis 配置类 |
| 40 | 43 | RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer |
| 41 | 44 | RedisAutoConfiguration.class, // Spring Redis 自动配置类 | ... | ... |
yudao-module-infra/yudao-module-infra-impl/src/test/java/cn/iocoder/yudao/module/infra/test/BaseDbUnitTest.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.infra.test; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 5 | 6 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 6 | 7 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 7 | 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| ... | ... | @@ -20,7 +21,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 20 | 21 | */ |
| 21 | 22 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) |
| 22 | 23 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 23 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 24 | 24 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 25 | 25 | public class BaseDbUnitTest { |
| 26 | 26 | |
| ... | ... | @@ -30,6 +30,7 @@ public class BaseDbUnitTest { |
| 30 | 30 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 31 | 31 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 32 | 32 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 33 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 33 | 34 | // MyBatis 配置类 |
| 34 | 35 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 35 | 36 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | ... | ... |
yudao-module-infra/yudao-module-infra-impl/src/test/java/cn/iocoder/yudao/module/infra/test/RedisTestConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.module.infra.test; | |
| 2 | - | |
| 3 | -import com.github.fppt.jedismock.RedisServer; | |
| 4 | -import org.springframework.boot.autoconfigure.data.redis.RedisProperties; | |
| 5 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 6 | -import org.springframework.context.annotation.Bean; | |
| 7 | -import org.springframework.context.annotation.Configuration; | |
| 8 | -import org.springframework.context.annotation.Lazy; | |
| 9 | - | |
| 10 | -import java.io.IOException; | |
| 11 | - | |
| 12 | -@Configuration(proxyBeanMethods = false) | |
| 13 | -@Lazy(false) // 禁止延迟加载 | |
| 14 | -@EnableConfigurationProperties(RedisProperties.class) | |
| 15 | -public class RedisTestConfiguration { | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * 创建模拟的 Redis Server 服务器 | |
| 19 | - */ | |
| 20 | - @Bean | |
| 21 | - public RedisServer redisServer(RedisProperties properties) throws IOException { | |
| 22 | - RedisServer redisServer = new RedisServer(properties.getPort()); | |
| 23 | - // TODO 芋艿:一次执行多个单元测试时,貌似创建多个 spring 容器,导致不进行 stop。这样,就导致端口被占用,无法启动。。。 | |
| 24 | - try { | |
| 25 | - redisServer.start(); | |
| 26 | - } catch (Exception ignore) {} | |
| 27 | - return redisServer; | |
| 28 | - } | |
| 29 | - | |
| 30 | -} |
yudao-module-infra/yudao-module-infra-impl/src/test/resources/application-unit-test.yaml
yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/test/BaseDbAndRedisUnitTest.java
| ... | ... | @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.member.test; |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | 5 | import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; |
| 6 | +import cn.iocoder.yudao.framework.test.config.RedisTestConfiguration; | |
| 7 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 6 | 8 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 7 | 9 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 8 | 10 | import org.redisson.spring.starter.RedissonAutoConfiguration; |
| ... | ... | @@ -23,7 +25,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 23 | 25 | */ |
| 24 | 26 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisUnitTest.Application.class) |
| 25 | 27 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 26 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 27 | 28 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 28 | 29 | public class BaseDbAndRedisUnitTest { |
| 29 | 30 | |
| ... | ... | @@ -33,9 +34,11 @@ public class BaseDbAndRedisUnitTest { |
| 33 | 34 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 34 | 35 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 35 | 36 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 37 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 36 | 38 | // MyBatis 配置类 |
| 37 | 39 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 38 | 40 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 |
| 41 | + | |
| 39 | 42 | // Redis 配置类 |
| 40 | 43 | RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer |
| 41 | 44 | RedisAutoConfiguration.class, // Spring Redis 自动配置类 | ... | ... |
yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/test/BaseDbUnitTest.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.member.test; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 5 | 6 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 6 | 7 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 7 | 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| ... | ... | @@ -20,7 +21,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 20 | 21 | */ |
| 21 | 22 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) |
| 22 | 23 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 23 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 24 | 24 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 25 | 25 | public class BaseDbUnitTest { |
| 26 | 26 | |
| ... | ... | @@ -30,6 +30,7 @@ public class BaseDbUnitTest { |
| 30 | 30 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 31 | 31 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 32 | 32 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 33 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 33 | 34 | // MyBatis 配置类 |
| 34 | 35 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 35 | 36 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | ... | ... |
yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/test/RedisTestConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.module.member.test; | |
| 2 | - | |
| 3 | -import com.github.fppt.jedismock.RedisServer; | |
| 4 | -import org.springframework.boot.autoconfigure.data.redis.RedisProperties; | |
| 5 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 6 | -import org.springframework.context.annotation.Bean; | |
| 7 | -import org.springframework.context.annotation.Configuration; | |
| 8 | -import org.springframework.context.annotation.Lazy; | |
| 9 | - | |
| 10 | -import java.io.IOException; | |
| 11 | - | |
| 12 | -@Configuration(proxyBeanMethods = false) | |
| 13 | -@Lazy(false) // 禁止延迟加载 | |
| 14 | -@EnableConfigurationProperties(RedisProperties.class) | |
| 15 | -public class RedisTestConfiguration { | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * 创建模拟的 Redis Server 服务器 | |
| 19 | - */ | |
| 20 | - @Bean | |
| 21 | - public RedisServer redisServer(RedisProperties properties) throws IOException { | |
| 22 | - RedisServer redisServer = new RedisServer(properties.getPort()); | |
| 23 | - // TODO 芋艿:一次执行多个单元测试时,貌似创建多个 spring 容器,导致不进行 stop。这样,就导致端口被占用,无法启动。。。 | |
| 24 | - try { | |
| 25 | - redisServer.start(); | |
| 26 | - } catch (Exception ignore) {} | |
| 27 | - return redisServer; | |
| 28 | - } | |
| 29 | - | |
| 30 | -} |
yudao-module-member/yudao-module-member-impl/src/test/resources/application-unit-test.yaml
yudao-module-pay/yudao-module-pay-impl/src/test/java/cn/iocoder/yudao/module/pay/test/BaseDbAndRedisUnitTest.java
| ... | ... | @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.pay.test; |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | 5 | import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; |
| 6 | +import cn.iocoder.yudao.framework.test.config.RedisTestConfiguration; | |
| 7 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 6 | 8 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 7 | 9 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 8 | 10 | import org.redisson.spring.starter.RedissonAutoConfiguration; |
| ... | ... | @@ -23,7 +25,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 23 | 25 | */ |
| 24 | 26 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisUnitTest.Application.class) |
| 25 | 27 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 26 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 27 | 28 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 28 | 29 | public class BaseDbAndRedisUnitTest { |
| 29 | 30 | |
| ... | ... | @@ -33,9 +34,11 @@ public class BaseDbAndRedisUnitTest { |
| 33 | 34 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 34 | 35 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 35 | 36 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 37 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 36 | 38 | // MyBatis 配置类 |
| 37 | 39 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 38 | 40 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 |
| 41 | + | |
| 39 | 42 | // Redis 配置类 |
| 40 | 43 | RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer |
| 41 | 44 | RedisAutoConfiguration.class, // Spring Redis 自动配置类 | ... | ... |
yudao-module-pay/yudao-module-pay-impl/src/test/java/cn/iocoder/yudao/module/pay/test/BaseDbUnitTest.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.pay.test; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 5 | 6 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 6 | 7 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 7 | 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| ... | ... | @@ -20,7 +21,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 20 | 21 | */ |
| 21 | 22 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) |
| 22 | 23 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 23 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 24 | 24 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 25 | 25 | public class BaseDbUnitTest { |
| 26 | 26 | |
| ... | ... | @@ -30,6 +30,7 @@ public class BaseDbUnitTest { |
| 30 | 30 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 31 | 31 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 32 | 32 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 33 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 33 | 34 | // MyBatis 配置类 |
| 34 | 35 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 35 | 36 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | ... | ... |
yudao-module-pay/yudao-module-pay-impl/src/test/java/cn/iocoder/yudao/module/pay/test/RedisTestConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.module.pay.test; | |
| 2 | - | |
| 3 | -import com.github.fppt.jedismock.RedisServer; | |
| 4 | -import org.springframework.boot.autoconfigure.data.redis.RedisProperties; | |
| 5 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 6 | -import org.springframework.context.annotation.Bean; | |
| 7 | -import org.springframework.context.annotation.Configuration; | |
| 8 | -import org.springframework.context.annotation.Lazy; | |
| 9 | - | |
| 10 | -import java.io.IOException; | |
| 11 | - | |
| 12 | -@Configuration(proxyBeanMethods = false) | |
| 13 | -@Lazy(false) // 禁止延迟加载 | |
| 14 | -@EnableConfigurationProperties(RedisProperties.class) | |
| 15 | -public class RedisTestConfiguration { | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * 创建模拟的 Redis Server 服务器 | |
| 19 | - */ | |
| 20 | - @Bean | |
| 21 | - public RedisServer redisServer(RedisProperties properties) throws IOException { | |
| 22 | - RedisServer redisServer = new RedisServer(properties.getPort()); | |
| 23 | - // TODO 芋艿:一次执行多个单元测试时,貌似创建多个 spring 容器,导致不进行 stop。这样,就导致端口被占用,无法启动。。。 | |
| 24 | - try { | |
| 25 | - redisServer.start(); | |
| 26 | - } catch (Exception ignore) {} | |
| 27 | - return redisServer; | |
| 28 | - } | |
| 29 | - | |
| 30 | -} |
yudao-module-pay/yudao-module-pay-impl/src/test/resources/application-unit-test.yaml
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/MenuController.java
| 1 | 1 | package cn.iocoder.yudao.module.system.controller.admin.permission; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.collection.CollUtil; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | 5 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.*; |
| ... | ... | @@ -74,12 +73,9 @@ public class MenuController { |
| 74 | 73 | // 获得菜单列表,只要开启状态的 |
| 75 | 74 | MenuListReqVO reqVO = new MenuListReqVO(); |
| 76 | 75 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 77 | - List<MenuDO> list = menuService.getMenus(reqVO); | |
| 76 | + List<MenuDO> list = menuService.getTenantMenus(reqVO); | |
| 78 | 77 | // 排序后,返回给前端 |
| 79 | 78 | list.sort(Comparator.comparing(MenuDO::getSort)); |
| 80 | - | |
| 81 | - // 开启多租户的情况下,需要过滤掉未开通的菜单 | |
| 82 | - tenantService.handleTenantMenu(menuIds -> list.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); | |
| 83 | 79 | return success(MenuConvert.INSTANCE.convertList02(list)); |
| 84 | 80 | } |
| 85 | 81 | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuService.java
| ... | ... | @@ -50,6 +50,15 @@ public interface MenuService { |
| 50 | 50 | List<MenuDO> getMenus(); |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | + * 基于租户,筛选菜单列表 | |
| 54 | + * 注意,如果是系统租户,返回的还是全菜单 | |
| 55 | + * | |
| 56 | + * @param reqVO 筛选条件请求 VO | |
| 57 | + * @return 菜单列表 | |
| 58 | + */ | |
| 59 | + List<MenuDO> getTenantMenus(MenuListReqVO reqVO); | |
| 60 | + | |
| 61 | + /** | |
| 53 | 62 | * 筛选菜单列表 |
| 54 | 63 | * |
| 55 | 64 | * @param reqVO 筛选条件请求 VO | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java
| ... | ... | @@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper; |
| 12 | 12 | import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum; |
| 13 | 13 | import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum; |
| 14 | 14 | import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer; |
| 15 | +import cn.iocoder.yudao.module.system.service.tenant.TenantService; | |
| 15 | 16 | import com.google.common.annotations.VisibleForTesting; |
| 16 | 17 | import com.google.common.collect.ImmutableMap; |
| 17 | 18 | import com.google.common.collect.ImmutableMultimap; |
| ... | ... | @@ -69,6 +70,8 @@ public class MenuServiceImpl implements MenuService { |
| 69 | 70 | private MenuMapper menuMapper; |
| 70 | 71 | @Resource |
| 71 | 72 | private PermissionService permissionService; |
| 73 | + @Resource | |
| 74 | + private TenantService tenantService; | |
| 72 | 75 | |
| 73 | 76 | @Resource |
| 74 | 77 | private MenuProducer menuProducer; |
| ... | ... | @@ -194,6 +197,14 @@ public class MenuServiceImpl implements MenuService { |
| 194 | 197 | } |
| 195 | 198 | |
| 196 | 199 | @Override |
| 200 | + public List<MenuDO> getTenantMenus(MenuListReqVO reqVO) { | |
| 201 | + List<MenuDO> menus = getMenus(reqVO); | |
| 202 | + // 开启多租户的情况下,需要过滤掉未开通的菜单 | |
| 203 | + tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); | |
| 204 | + return menus; | |
| 205 | + } | |
| 206 | + | |
| 207 | + @Override | |
| 197 | 208 | public List<MenuDO> getMenus(MenuListReqVO reqVO) { |
| 198 | 209 | return menuMapper.selectList(reqVO); |
| 199 | 210 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java
| ... | ... | @@ -19,6 +19,7 @@ import cn.iocoder.yudao.module.system.enums.permission.RoleCodeEnum; |
| 19 | 19 | import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum; |
| 20 | 20 | import cn.iocoder.yudao.module.system.mq.producer.permission.RoleProducer; |
| 21 | 21 | import com.google.common.annotations.VisibleForTesting; |
| 22 | +import lombok.Getter; | |
| 22 | 23 | import lombok.extern.slf4j.Slf4j; |
| 23 | 24 | import org.springframework.context.annotation.Lazy; |
| 24 | 25 | import org.springframework.lang.Nullable; |
| ... | ... | @@ -58,10 +59,12 @@ public class RoleServiceImpl implements RoleService { |
| 58 | 59 | * |
| 59 | 60 | * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向 |
| 60 | 61 | */ |
| 62 | + @Getter | |
| 61 | 63 | private volatile Map<Long, RoleDO> roleCache; |
| 62 | 64 | /** |
| 63 | 65 | * 缓存角色的最大更新时间,用于后续的增量轮询,判断是否有更新 |
| 64 | 66 | */ |
| 67 | + @Getter | |
| 65 | 68 | private volatile Date maxUpdateTime; |
| 66 | 69 | |
| 67 | 70 | @Resource |
| ... | ... | @@ -144,6 +147,7 @@ public class RoleServiceImpl implements RoleService { |
| 144 | 147 | checkUpdateRole(reqVO.getId()); |
| 145 | 148 | // 校验角色的唯一字段是否重复 |
| 146 | 149 | checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); |
| 150 | + | |
| 147 | 151 | // 更新到数据库 |
| 148 | 152 | RoleDO updateObject = RoleConvert.INSTANCE.convert(reqVO); |
| 149 | 153 | roleMapper.updateById(updateObject); | ... | ... |
yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceTest.java
| ... | ... | @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.system.service.permission; |
| 3 | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | 4 | import cn.hutool.core.lang.Assert; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 6 | +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 7 | +import cn.iocoder.yudao.framework.common.util.spring.SpringAopUtils; | |
| 6 | 8 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuCreateReqVO; |
| 7 | 9 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| 8 | 10 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUpdateReqVO; |
| ... | ... | @@ -10,8 +12,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| 10 | 12 | import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper; |
| 11 | 13 | import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum; |
| 12 | 14 | import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer; |
| 13 | -import cn.iocoder.yudao.framework.common.util.spring.SpringAopUtils; | |
| 14 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 15 | +import cn.iocoder.yudao.module.system.service.tenant.TenantService; | |
| 15 | 16 | import cn.iocoder.yudao.module.system.test.BaseDbUnitTest; |
| 16 | 17 | import com.google.common.collect.Multimap; |
| 17 | 18 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -21,28 +22,33 @@ import org.springframework.context.annotation.Import; |
| 21 | 22 | import javax.annotation.Resource; |
| 22 | 23 | import java.util.*; |
| 23 | 24 | |
| 24 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 25 | +import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | |
| 25 | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 26 | 27 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 27 | 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 29 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 28 | 30 | import static org.junit.jupiter.api.Assertions.*; |
| 31 | +import static org.mockito.ArgumentMatchers.argThat; | |
| 32 | +import static org.mockito.Mockito.doNothing; | |
| 29 | 33 | import static org.mockito.Mockito.verify; |
| 30 | 34 | |
| 35 | +// TODO @芋艿:单测的代码质量可以提升下 | |
| 31 | 36 | @Import(MenuServiceImpl.class) |
| 32 | 37 | public class MenuServiceTest extends BaseDbUnitTest { |
| 33 | 38 | |
| 34 | 39 | @Resource |
| 35 | - private MenuServiceImpl sysMenuService; | |
| 36 | - | |
| 37 | - @MockBean | |
| 38 | - private PermissionService sysPermissionService; | |
| 39 | - | |
| 40 | - @MockBean | |
| 41 | - private MenuProducer sysMenuProducer; | |
| 40 | + private MenuServiceImpl menuService; | |
| 42 | 41 | |
| 43 | 42 | @Resource |
| 44 | 43 | private MenuMapper menuMapper; |
| 45 | 44 | |
| 45 | + @MockBean | |
| 46 | + private PermissionService permissionService; | |
| 47 | + @MockBean | |
| 48 | + private MenuProducer menuProducer; | |
| 49 | + @MockBean | |
| 50 | + private TenantService tenantService; | |
| 51 | + | |
| 46 | 52 | @Test |
| 47 | 53 | public void testInitLocalCache_success() throws Exception { |
| 48 | 54 | MenuDO menuDO1 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L); |
| ... | ... | @@ -51,10 +57,10 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 51 | 57 | menuMapper.insert(menuDO2); |
| 52 | 58 | |
| 53 | 59 | // 调用 |
| 54 | - sysMenuService.initLocalCache(); | |
| 60 | + menuService.initLocalCache(); | |
| 55 | 61 | |
| 56 | 62 | // 获取代理对象 |
| 57 | - MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(sysMenuService); | |
| 63 | + MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | |
| 58 | 64 | |
| 59 | 65 | Map<Long, MenuDO> menuCache = |
| 60 | 66 | (Map<Long, MenuDO>) BeanUtil.getFieldValue(target, "menuCache"); |
| ... | ... | @@ -86,7 +92,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 86 | 92 | o.setType(MenuTypeEnum.MENU.getType()); |
| 87 | 93 | o.setStatus(randomCommonStatus()); |
| 88 | 94 | }); |
| 89 | - Long menuId = sysMenuService.createMenu(vo); | |
| 95 | + Long menuId = menuService.createMenu(vo); | |
| 90 | 96 | |
| 91 | 97 | //断言 |
| 92 | 98 | assertNotNull(menuId); |
| ... | ... | @@ -94,7 +100,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 94 | 100 | MenuDO ret = menuMapper.selectById(menuId); |
| 95 | 101 | assertPojoEquals(vo, ret); |
| 96 | 102 | // 校验调用 |
| 97 | - verify(sysMenuProducer).sendMenuRefreshMessage(); | |
| 103 | + verify(menuProducer).sendMenuRefreshMessage(); | |
| 98 | 104 | } |
| 99 | 105 | |
| 100 | 106 | @Test |
| ... | ... | @@ -112,14 +118,14 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 112 | 118 | o.setStatus(randomCommonStatus()); |
| 113 | 119 | o.setName("pppppp"); //修改名字 |
| 114 | 120 | }); |
| 115 | - sysMenuService.updateMenu(vo); | |
| 121 | + menuService.updateMenu(vo); | |
| 116 | 122 | |
| 117 | 123 | //断言 |
| 118 | 124 | // 校验记录的属性是否正确 |
| 119 | 125 | MenuDO ret = menuMapper.selectById(sonId); |
| 120 | 126 | assertPojoEquals(vo, ret); |
| 121 | 127 | // 校验调用 |
| 122 | - verify(sysMenuProducer).sendMenuRefreshMessage(); | |
| 128 | + verify(menuProducer).sendMenuRefreshMessage(); | |
| 123 | 129 | } |
| 124 | 130 | |
| 125 | 131 | @Test |
| ... | ... | @@ -135,7 +141,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 135 | 141 | o.setStatus(randomCommonStatus()); |
| 136 | 142 | }); |
| 137 | 143 | //断言 |
| 138 | - assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS); | |
| 144 | + assertServiceException(() -> menuService.updateMenu(vo), MENU_NOT_EXISTS); | |
| 139 | 145 | } |
| 140 | 146 | |
| 141 | 147 | @Test |
| ... | ... | @@ -144,20 +150,20 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 144 | 150 | Long sonId = sonMenuDO.getId(); |
| 145 | 151 | |
| 146 | 152 | // 调用 |
| 147 | - sysMenuService.deleteMenu(sonId); | |
| 153 | + menuService.deleteMenu(sonId); | |
| 148 | 154 | |
| 149 | 155 | // 断言 |
| 150 | 156 | MenuDO menuDO = menuMapper.selectById(sonId); |
| 151 | 157 | assertNull(menuDO); |
| 152 | - verify(sysPermissionService).processMenuDeleted(sonId); | |
| 153 | - verify(sysMenuProducer).sendMenuRefreshMessage(); | |
| 158 | + verify(permissionService).processMenuDeleted(sonId); | |
| 159 | + verify(menuProducer).sendMenuRefreshMessage(); | |
| 154 | 160 | } |
| 155 | 161 | |
| 156 | 162 | @Test |
| 157 | 163 | public void testDeleteMenu_menuNotExist() { |
| 158 | 164 | Long sonId = 99999L; |
| 159 | 165 | |
| 160 | - assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS); | |
| 166 | + assertServiceException(() -> menuService.deleteMenu(sonId), MENU_NOT_EXISTS); | |
| 161 | 167 | } |
| 162 | 168 | |
| 163 | 169 | @Test |
| ... | ... | @@ -165,26 +171,50 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 165 | 171 | MenuDO sonMenu = initParentAndSonMenuDO(); |
| 166 | 172 | Long parentId = sonMenu.getParentId(); |
| 167 | 173 | |
| 168 | - assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | |
| 174 | + assertServiceException(() -> menuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | |
| 169 | 175 | } |
| 170 | 176 | |
| 171 | 177 | @Test |
| 172 | - public void testGetMenus_success() { | |
| 173 | - Map<Long, MenuDO> idMenuMap = new HashMap<>(); | |
| 174 | - MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | |
| 175 | - menuMapper.insert(menuDO); | |
| 176 | - idMenuMap.put(menuDO.getId(), menuDO); | |
| 177 | - | |
| 178 | - MenuDO sonMenu = createMenuDO(MenuTypeEnum.MENU, "son", menuDO.getId()); | |
| 179 | - menuMapper.insert(sonMenu); | |
| 180 | - idMenuMap.put(sonMenu.getId(), sonMenu); | |
| 178 | + public void testGetMenus() { | |
| 179 | + // mock 数据 | |
| 180 | + MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 181 | + menuMapper.insert(menu100); | |
| 182 | + MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 183 | + menuMapper.insert(menu101); | |
| 184 | + // 准备参数 | |
| 185 | + MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 181 | 186 | |
| 182 | 187 | // 调用 |
| 183 | - List<MenuDO> menuDOS = sysMenuService.getMenus(); | |
| 188 | + List<MenuDO> result = menuService.getMenus(reqVO); | |
| 189 | + // 断言 | |
| 190 | + assertEquals(1, result.size()); | |
| 191 | + assertPojoEquals(menu100, result.get(0)); | |
| 192 | + } | |
| 193 | + | |
| 194 | + @Test | |
| 195 | + public void testTenantMenus() { | |
| 196 | + // mock 数据 | |
| 197 | + MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 198 | + menuMapper.insert(menu100); | |
| 199 | + MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 200 | + menuMapper.insert(menu101); | |
| 201 | + MenuDO menu102 = randomPojo(MenuDO.class, o -> o.setId(102L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 202 | + menuMapper.insert(menu102); | |
| 203 | + // mock 过滤菜单 | |
| 204 | + // mock 账户额度充足 | |
| 205 | + Set<Long> menuIds = asSet(100L, 101L); | |
| 206 | + doNothing().when(tenantService).handleTenantMenu(argThat(handler -> { | |
| 207 | + handler.handle(menuIds); | |
| 208 | + return true; | |
| 209 | + })); | |
| 210 | + // 准备参数 | |
| 211 | + MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 184 | 212 | |
| 213 | + // 调用 | |
| 214 | + List<MenuDO> result = menuService.getTenantMenus(reqVO); | |
| 185 | 215 | // 断言 |
| 186 | - assertEquals(menuDOS.size(), idMenuMap.size()); | |
| 187 | - menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | |
| 216 | + assertEquals(1, result.size()); | |
| 217 | + assertPojoEquals(menu100, result.get(0)); | |
| 188 | 218 | } |
| 189 | 219 | |
| 190 | 220 | @Test |
| ... | ... | @@ -213,7 +243,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 213 | 243 | MenuListReqVO reqVO = new MenuListReqVO(); |
| 214 | 244 | reqVO.setStatus(1); |
| 215 | 245 | reqVO.setName("name"); |
| 216 | - List<MenuDO> menuDOS = sysMenuService.getMenus(reqVO); | |
| 246 | + List<MenuDO> menuDOS = menuService.getMenus(reqVO); | |
| 217 | 247 | |
| 218 | 248 | // 断言 |
| 219 | 249 | assertEquals(menuDOS.size(), idMenuMap.size()); |
| ... | ... | @@ -224,7 +254,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 224 | 254 | public void testListMenusFromCache_success() throws Exception { |
| 225 | 255 | Map<Long, MenuDO> mockCacheMap = new HashMap<>(); |
| 226 | 256 | // 获取代理对象 |
| 227 | - MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(sysMenuService); | |
| 257 | + MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | |
| 228 | 258 | BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); |
| 229 | 259 | |
| 230 | 260 | Map<Long, MenuDO> idMenuMap = new HashMap<>(); |
| ... | ... | @@ -243,7 +273,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 243 | 273 | menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); |
| 244 | 274 | mockCacheMap.put(menuDO.getId(), menuDO); |
| 245 | 275 | |
| 246 | - List<MenuDO> menuDOS = sysMenuService.getMenuListFromCache(Collections.singletonList(MenuTypeEnum.MENU.getType()), | |
| 276 | + List<MenuDO> menuDOS = menuService.getMenuListFromCache(Collections.singletonList(MenuTypeEnum.MENU.getType()), | |
| 247 | 277 | Collections.singletonList(CommonStatusEnum.DISABLE.getStatus())); |
| 248 | 278 | assertEquals(menuDOS.size(), idMenuMap.size()); |
| 249 | 279 | menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); |
| ... | ... | @@ -253,7 +283,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 253 | 283 | public void testListMenusFromCache2_success() throws Exception { |
| 254 | 284 | Map<Long, MenuDO> mockCacheMap = new HashMap<>(); |
| 255 | 285 | // 获取代理对象 |
| 256 | - MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(sysMenuService); | |
| 286 | + MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | |
| 257 | 287 | BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); |
| 258 | 288 | |
| 259 | 289 | Map<Long, MenuDO> idMenuMap = new HashMap<>(); |
| ... | ... | @@ -270,7 +300,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 270 | 300 | menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); |
| 271 | 301 | mockCacheMap.put(menuDO.getId(), menuDO); |
| 272 | 302 | |
| 273 | - List<MenuDO> menuDOS = sysMenuService.getMenuListFromCache(Collections.singletonList(1L), | |
| 303 | + List<MenuDO> menuDOS = menuService.getMenuListFromCache(Collections.singletonList(1L), | |
| 274 | 304 | Collections.singletonList(MenuTypeEnum.MENU.getType()), Collections.singletonList(1)); |
| 275 | 305 | assertEquals(menuDOS.size(), idMenuMap.size()); |
| 276 | 306 | menuDOS.forEach(menu -> assertPojoEquals(idMenuMap.get(menu.getId()), menu)); |
| ... | ... | @@ -282,17 +312,17 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 282 | 312 | menuMapper.insert(menuDO); |
| 283 | 313 | Long parentId = menuDO.getId(); |
| 284 | 314 | |
| 285 | - sysMenuService.checkParentResource(parentId, null); | |
| 315 | + menuService.checkParentResource(parentId, null); | |
| 286 | 316 | } |
| 287 | 317 | |
| 288 | 318 | @Test |
| 289 | 319 | public void testCheckParentResource_canNotSetSelfToBeParent() { |
| 290 | - assertServiceException(() -> sysMenuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); | |
| 320 | + assertServiceException(() -> menuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); | |
| 291 | 321 | } |
| 292 | 322 | |
| 293 | 323 | @Test |
| 294 | 324 | public void testCheckParentResource_parentNotExist() { |
| 295 | - assertServiceException(() -> sysMenuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); | |
| 325 | + assertServiceException(() -> menuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); | |
| 296 | 326 | } |
| 297 | 327 | |
| 298 | 328 | @Test |
| ... | ... | @@ -301,7 +331,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 301 | 331 | menuMapper.insert(menuDO); |
| 302 | 332 | Long parentId = menuDO.getId(); |
| 303 | 333 | |
| 304 | - assertServiceException(() -> sysMenuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); | |
| 334 | + assertServiceException(() -> menuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); | |
| 305 | 335 | } |
| 306 | 336 | |
| 307 | 337 | @Test |
| ... | ... | @@ -312,7 +342,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 312 | 342 | Long otherSonMenuId = randomLongId(); |
| 313 | 343 | String otherSonMenuName = randomString(); |
| 314 | 344 | |
| 315 | - sysMenuService.checkResource(parentId, otherSonMenuName, otherSonMenuId); | |
| 345 | + menuService.checkResource(parentId, otherSonMenuName, otherSonMenuId); | |
| 316 | 346 | } |
| 317 | 347 | |
| 318 | 348 | @Test |
| ... | ... | @@ -323,7 +353,7 @@ public class MenuServiceTest extends BaseDbUnitTest { |
| 323 | 353 | Long otherSonMenuId=randomLongId(); |
| 324 | 354 | String otherSonMenuName=sonMenu.getName(); //相同名称 |
| 325 | 355 | |
| 326 | - assertServiceException(() -> sysMenuService.checkResource(parentId, otherSonMenuName, otherSonMenuId), MENU_NAME_DUPLICATE); | |
| 356 | + assertServiceException(() -> menuService.checkResource(parentId, otherSonMenuName, otherSonMenuId), MENU_NAME_DUPLICATE); | |
| 327 | 357 | } |
| 328 | 358 | |
| 329 | 359 | /** | ... | ... |
yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java
yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.permission; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.bean.BeanUtil; | |
| 3 | +import cn.hutool.core.util.RandomUtil; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 6 | import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; |
| ... | ... | @@ -11,7 +11,6 @@ import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| 11 | 11 | import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMapper; |
| 12 | 12 | import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum; |
| 13 | 13 | import cn.iocoder.yudao.module.system.mq.producer.permission.RoleProducer; |
| 14 | -import cn.iocoder.yudao.framework.common.util.spring.SpringAopUtils; | |
| 15 | 14 | import cn.iocoder.yudao.module.system.test.BaseDbUnitTest; |
| 16 | 15 | import org.junit.jupiter.api.Test; |
| 17 | 16 | import org.springframework.boot.test.mock.mockito.MockBean; |
| ... | ... | @@ -21,71 +20,67 @@ import javax.annotation.Resource; |
| 21 | 20 | import java.util.*; |
| 22 | 21 | import java.util.stream.Collectors; |
| 23 | 22 | |
| 24 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 23 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max; | |
| 25 | 24 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 26 | 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 27 | -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max; | |
| 28 | 26 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 27 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | |
| 29 | 28 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | 29 | import static org.mockito.Mockito.verify; |
| 31 | 30 | |
| 31 | +// TODO @芋艿:单测的代码质量可以提升下 | |
| 32 | 32 | @Import(RoleServiceImpl.class) |
| 33 | 33 | public class RoleServiceTest extends BaseDbUnitTest { |
| 34 | 34 | |
| 35 | 35 | @Resource |
| 36 | - private RoleServiceImpl sysRoleService; | |
| 36 | + private RoleServiceImpl roleService; | |
| 37 | 37 | |
| 38 | 38 | @Resource |
| 39 | 39 | private RoleMapper roleMapper; |
| 40 | 40 | |
| 41 | 41 | @MockBean |
| 42 | - private PermissionService sysPermissionService; | |
| 43 | - | |
| 42 | + private PermissionService permissionService; | |
| 44 | 43 | @MockBean |
| 45 | - private RoleProducer sysRoleProducer; | |
| 44 | + private RoleProducer roleProducer; | |
| 46 | 45 | |
| 47 | 46 | @Test |
| 48 | - public void testInitLocalCache_success() throws Exception { | |
| 49 | - RoleDO roleDO1 = createRoleDO("role1", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 47 | + public void testInitLocalCache() { | |
| 48 | + RoleDO roleDO1 = randomRole(); | |
| 50 | 49 | roleMapper.insert(roleDO1); |
| 51 | - RoleDO roleDO2 = createRoleDO("role2", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 50 | + RoleDO roleDO2 = randomRole(); | |
| 52 | 51 | roleMapper.insert(roleDO2); |
| 53 | 52 | |
| 54 | - //调用 | |
| 55 | - sysRoleService.initLocalCache(); | |
| 56 | - | |
| 57 | - //断言 | |
| 58 | - //获取代理对象 | |
| 59 | - RoleServiceImpl target = (RoleServiceImpl) SpringAopUtils.getTarget(sysRoleService); | |
| 60 | - | |
| 61 | - Map<Long, RoleDO> roleCache = (Map<Long, RoleDO>) BeanUtil.getFieldValue(target, "roleCache"); | |
| 53 | + // 调用 | |
| 54 | + roleService.initLocalCache(); | |
| 55 | + // 断言 roleCache 缓存 | |
| 56 | + Map<Long, RoleDO> roleCache = roleService.getRoleCache(); | |
| 62 | 57 | assertPojoEquals(roleDO1, roleCache.get(roleDO1.getId())); |
| 63 | 58 | assertPojoEquals(roleDO2, roleCache.get(roleDO2.getId())); |
| 64 | - | |
| 65 | - Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime"); | |
| 66 | - assertEquals(max(roleDO1.getUpdateTime(), roleDO2.getUpdateTime()), maxUpdateTime); | |
| 59 | + // 断言 maxUpdateTime 缓存 | |
| 60 | + assertEquals(max(roleDO1.getUpdateTime(), roleDO2.getUpdateTime()), roleService.getMaxUpdateTime()); | |
| 67 | 61 | } |
| 68 | 62 | |
| 69 | 63 | @Test |
| 70 | 64 | public void testCreateRole_success() { |
| 71 | - RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class, o -> { | |
| 72 | - o.setCode("role_code"); | |
| 73 | - o.setName("role_name"); | |
| 74 | - o.setRemark("remark"); | |
| 75 | - o.setSort(1); | |
| 76 | - }); | |
| 77 | - Long roleId = sysRoleService.createRole(reqVO, null); | |
| 65 | + // 准备参数 | |
| 66 | + RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class); | |
| 78 | 67 | |
| 79 | - //断言 | |
| 68 | + // 调用 | |
| 69 | + Long roleId = roleService.createRole(reqVO, null); | |
| 70 | + // 断言 | |
| 80 | 71 | assertNotNull(roleId); |
| 81 | 72 | RoleDO roleDO = roleMapper.selectById(roleId); |
| 82 | 73 | assertPojoEquals(reqVO, roleDO); |
| 83 | - | |
| 84 | - verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 74 | + assertEquals(RoleTypeEnum.CUSTOM.getType(), roleDO.getType()); | |
| 75 | + assertEquals(CommonStatusEnum.ENABLE.getStatus(), roleDO.getStatus()); | |
| 76 | + assertEquals(DataScopeEnum.ALL.getScope(), roleDO.getDataScope()); | |
| 77 | + // verify 发送刷新消息 | |
| 78 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 85 | 79 | } |
| 86 | 80 | |
| 87 | 81 | @Test |
| 88 | 82 | public void testUpdateRole_success() { |
| 83 | + // mock 数据 | |
| 89 | 84 | RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); |
| 90 | 85 | roleMapper.insert(roleDO); |
| 91 | 86 | Long roleId = roleDO.getId(); |
| ... | ... | @@ -97,13 +92,13 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 97 | 92 | o.setName("update_name"); |
| 98 | 93 | o.setSort(999); |
| 99 | 94 | }); |
| 100 | - sysRoleService.updateRole(reqVO); | |
| 95 | + roleService.updateRole(reqVO); | |
| 101 | 96 | |
| 102 | 97 | //断言 |
| 103 | 98 | RoleDO newRoleDO = roleMapper.selectById(roleId); |
| 104 | 99 | assertPojoEquals(reqVO, newRoleDO); |
| 105 | 100 | |
| 106 | - verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 101 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 107 | 102 | } |
| 108 | 103 | |
| 109 | 104 | @Test |
| ... | ... | @@ -113,13 +108,13 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 113 | 108 | Long roleId = roleDO.getId(); |
| 114 | 109 | |
| 115 | 110 | //调用 |
| 116 | - sysRoleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); | |
| 111 | + roleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); | |
| 117 | 112 | |
| 118 | 113 | //断言 |
| 119 | 114 | RoleDO newRoleDO = roleMapper.selectById(roleId); |
| 120 | 115 | assertEquals(CommonStatusEnum.DISABLE.getStatus(), newRoleDO.getStatus()); |
| 121 | 116 | |
| 122 | - verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 117 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 123 | 118 | } |
| 124 | 119 | |
| 125 | 120 | @Test |
| ... | ... | @@ -130,7 +125,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 130 | 125 | |
| 131 | 126 | //调用 |
| 132 | 127 | Set<Long> deptIdSet = Arrays.asList(1L, 2L, 3L, 4L, 5L).stream().collect(Collectors.toSet()); |
| 133 | - sysRoleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); | |
| 128 | + roleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); | |
| 134 | 129 | |
| 135 | 130 | //断言 |
| 136 | 131 | RoleDO newRoleDO = roleMapper.selectById(roleId); |
| ... | ... | @@ -140,7 +135,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 140 | 135 | assertTrue(deptIdSet.size() == newDeptIdSet.size()); |
| 141 | 136 | deptIdSet.stream().forEach(d -> assertTrue(newDeptIdSet.contains(d))); |
| 142 | 137 | |
| 143 | - verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 138 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 144 | 139 | } |
| 145 | 140 | |
| 146 | 141 | @Test |
| ... | ... | @@ -150,13 +145,13 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 150 | 145 | Long roleId = roleDO.getId(); |
| 151 | 146 | |
| 152 | 147 | //调用 |
| 153 | - sysRoleService.deleteRole(roleId); | |
| 148 | + roleService.deleteRole(roleId); | |
| 154 | 149 | |
| 155 | 150 | //断言 |
| 156 | 151 | RoleDO newRoleDO = roleMapper.selectById(roleId); |
| 157 | 152 | assertNull(newRoleDO); |
| 158 | 153 | |
| 159 | - verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 154 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 160 | 155 | } |
| 161 | 156 | |
| 162 | 157 | @Test |
| ... | ... | @@ -176,7 +171,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 176 | 171 | roleMapper.insert(roleDO3); |
| 177 | 172 | |
| 178 | 173 | //调用 |
| 179 | - List<RoleDO> roles = sysRoleService.getRoles(Arrays.asList(1)); | |
| 174 | + List<RoleDO> roles = roleService.getRoles(Arrays.asList(1)); | |
| 180 | 175 | |
| 181 | 176 | //断言 |
| 182 | 177 | assertEquals(2, roles.size()); |
| ... | ... | @@ -212,14 +207,14 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 212 | 207 | o.setBeginTime(null); |
| 213 | 208 | o.setEndTime(null); |
| 214 | 209 | }); |
| 215 | - PageResult<RoleDO> result = sysRoleService.getRolePage(reqVO); | |
| 210 | + PageResult<RoleDO> result = roleService.getRolePage(reqVO); | |
| 216 | 211 | assertEquals(2, result.getTotal()); |
| 217 | 212 | result.getList().stream().forEach(r -> assertPojoEquals(idRoleMap.get(r.getId()), r)); |
| 218 | 213 | } |
| 219 | 214 | |
| 220 | 215 | @Test |
| 221 | 216 | public void testCheckDuplicateRole_success() { |
| 222 | - sysRoleService.checkDuplicateRole(randomString(), randomString(), null); | |
| 217 | + roleService.checkDuplicateRole(randomString(), randomString(), null); | |
| 223 | 218 | } |
| 224 | 219 | |
| 225 | 220 | @Test |
| ... | ... | @@ -229,7 +224,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 229 | 224 | |
| 230 | 225 | String duplicateName = "role_name"; |
| 231 | 226 | |
| 232 | - assertServiceException(() -> sysRoleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | |
| 227 | + assertServiceException(() -> roleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | |
| 233 | 228 | } |
| 234 | 229 | |
| 235 | 230 | @Test |
| ... | ... | @@ -246,7 +241,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 246 | 241 | String randomName = randomString(); |
| 247 | 242 | String duplicateCode = "code"; |
| 248 | 243 | |
| 249 | - assertServiceException(() -> sysRoleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | |
| 244 | + assertServiceException(() -> roleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | |
| 250 | 245 | } |
| 251 | 246 | |
| 252 | 247 | @Test |
| ... | ... | @@ -255,12 +250,12 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 255 | 250 | roleMapper.insert(roleDO); |
| 256 | 251 | Long roleId = roleDO.getId(); |
| 257 | 252 | |
| 258 | - sysRoleService.checkUpdateRole(roleId); | |
| 253 | + roleService.checkUpdateRole(roleId); | |
| 259 | 254 | } |
| 260 | 255 | |
| 261 | 256 | @Test |
| 262 | 257 | public void testCheckUpdateRole_roleIdNotExist() { |
| 263 | - assertServiceException(() -> sysRoleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | |
| 258 | + assertServiceException(() -> roleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | |
| 264 | 259 | } |
| 265 | 260 | |
| 266 | 261 | @Test |
| ... | ... | @@ -269,7 +264,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 269 | 264 | roleMapper.insert(roleDO); |
| 270 | 265 | Long roleId = roleDO.getId(); |
| 271 | 266 | |
| 272 | - assertServiceException(() -> sysRoleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 267 | + assertServiceException(() -> roleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 273 | 268 | } |
| 274 | 269 | |
| 275 | 270 | private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status) { |
| ... | ... | @@ -285,7 +280,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 285 | 280 | } |
| 286 | 281 | |
| 287 | 282 | private RoleDO createRoleDO(Long id, String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { |
| 288 | - RoleDO roleDO = randomPojo(RoleDO.class, o -> { | |
| 283 | + return randomPojo(RoleDO.class, o -> { | |
| 289 | 284 | o.setId(id); |
| 290 | 285 | o.setName(name); |
| 291 | 286 | o.setType(typeEnum.getType()); |
| ... | ... | @@ -293,7 +288,11 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 293 | 288 | o.setDataScope(scopeEnum.getScope()); |
| 294 | 289 | o.setCode(code); |
| 295 | 290 | }); |
| 296 | - return roleDO; | |
| 291 | + } | |
| 292 | + | |
| 293 | + private RoleDO randomRole() { | |
| 294 | + return randomPojo(RoleDO.class, | |
| 295 | + o -> o.setDataScope(RandomUtil.randomEle(DataScopeEnum.values()).getScope())); | |
| 297 | 296 | } |
| 298 | 297 | |
| 299 | 298 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/user/UserServiceImplTest.java
| ... | ... | @@ -13,12 +13,14 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfi |
| 13 | 13 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; |
| 14 | 14 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; |
| 15 | 15 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; |
| 16 | +import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; | |
| 16 | 17 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 17 | 18 | import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; |
| 18 | 19 | import cn.iocoder.yudao.module.system.enums.common.SexEnum; |
| 19 | 20 | import cn.iocoder.yudao.module.system.service.dept.DeptService; |
| 20 | 21 | import cn.iocoder.yudao.module.system.service.dept.PostService; |
| 21 | 22 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; |
| 23 | +import cn.iocoder.yudao.module.system.service.tenant.TenantService; | |
| 22 | 24 | import cn.iocoder.yudao.module.system.test.BaseDbUnitTest; |
| 23 | 25 | import org.junit.jupiter.api.Test; |
| 24 | 26 | import org.mockito.stubbing.Answer; |
| ... | ... | @@ -55,7 +57,6 @@ public class UserServiceImplTest extends BaseDbUnitTest { |
| 55 | 57 | |
| 56 | 58 | @MockBean |
| 57 | 59 | private DeptService deptService; |
| 58 | - | |
| 59 | 60 | @MockBean |
| 60 | 61 | private PostService postService; |
| 61 | 62 | @MockBean |
| ... | ... | @@ -63,6 +64,8 @@ public class UserServiceImplTest extends BaseDbUnitTest { |
| 63 | 64 | @MockBean |
| 64 | 65 | private PasswordEncoder passwordEncoder; |
| 65 | 66 | @MockBean |
| 67 | + private TenantService tenantService; | |
| 68 | + @MockBean | |
| 66 | 69 | private FileApi fileApi; |
| 67 | 70 | |
| 68 | 71 | @Test |
| ... | ... | @@ -72,6 +75,12 @@ public class UserServiceImplTest extends BaseDbUnitTest { |
| 72 | 75 | o.setSex(RandomUtil.randomEle(SexEnum.values()).getSex()); |
| 73 | 76 | o.setMobile(randomString()); |
| 74 | 77 | }); |
| 78 | + // mock 账户额度充足 | |
| 79 | + TenantDO tenant = randomPojo(TenantDO.class, o -> o.setAccountCount(1)); | |
| 80 | + doNothing().when(tenantService).handleTenantInfo(argThat(handler -> { | |
| 81 | + handler.handle(tenant); | |
| 82 | + return true; | |
| 83 | + })); | |
| 75 | 84 | // mock deptService 的方法 |
| 76 | 85 | DeptDO dept = randomPojo(DeptDO.class, o -> { |
| 77 | 86 | o.setId(reqVO.getDeptId()); |
| ... | ... | @@ -98,6 +107,21 @@ public class UserServiceImplTest extends BaseDbUnitTest { |
| 98 | 107 | } |
| 99 | 108 | |
| 100 | 109 | @Test |
| 110 | + public void testCreatUser_max() { | |
| 111 | + // 准备参数 | |
| 112 | + UserCreateReqVO reqVO = randomPojo(UserCreateReqVO.class); | |
| 113 | + // mock 账户额度不足 | |
| 114 | + TenantDO tenant = randomPojo(TenantDO.class, o -> o.setAccountCount(-1)); | |
| 115 | + doNothing().when(tenantService).handleTenantInfo(argThat(handler -> { | |
| 116 | + handler.handle(tenant); | |
| 117 | + return true; | |
| 118 | + })); | |
| 119 | + | |
| 120 | + // 调用,并断言异常 | |
| 121 | + assertServiceException(() -> userService.createUser(reqVO), USER_COUNT_MAX, -1); | |
| 122 | + } | |
| 123 | + | |
| 124 | + @Test | |
| 101 | 125 | public void testUpdateUser_success() { |
| 102 | 126 | // mock 数据 |
| 103 | 127 | AdminUserDO dbUser = randomAdminUserDO(); | ... | ... |
yudao-module-system/yudao-module-system-impl/src/test/resources/sql/create_tables.sql
| ... | ... | @@ -49,6 +49,7 @@ CREATE TABLE IF NOT EXISTS "system_role" ( |
| 49 | 49 | "updater" varchar(64) DEFAULT '', |
| 50 | 50 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 51 | 51 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 52 | + "tenant_id" bigint not null default '0', | |
| 52 | 53 | PRIMARY KEY ("id") |
| 53 | 54 | ) COMMENT '角色信息表'; |
| 54 | 55 | |
| ... | ... | @@ -61,6 +62,7 @@ CREATE TABLE IF NOT EXISTS "system_role_menu" ( |
| 61 | 62 | "updater" varchar(64) DEFAULT '', |
| 62 | 63 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 63 | 64 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 65 | + "tenant_id" bigint not null default '0', | |
| 64 | 66 | PRIMARY KEY ("id") |
| 65 | 67 | ) COMMENT '角色和菜单关联表'; |
| 66 | 68 | |
| ... | ... | @@ -92,7 +94,8 @@ CREATE TABLE IF NOT EXISTS "system_user_role" ( |
| 92 | 94 | "updater" varchar(64) DEFAULT '', |
| 93 | 95 | "update_time" timestamp DEFAULT NULL, |
| 94 | 96 | "deleted" bit DEFAULT FALSE, |
| 95 | - PRIMARY KEY ("id") | |
| 97 | + "tenant_id" bigint not null default '0', | |
| 98 | + PRIMARY KEY ("id") | |
| 96 | 99 | ) COMMENT '用户和角色关联表'; |
| 97 | 100 | |
| 98 | 101 | CREATE TABLE IF NOT EXISTS "system_dict_type" ( | ... | ... |
yudao-module-tool/yudao-module-tool-impl/src/test/java/cn/iocoder/yudao/module/tool/test/BaseDbUnitTest.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.tool.test; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; |
| 5 | +import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; | |
| 5 | 6 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 6 | 7 | import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; |
| 7 | 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| ... | ... | @@ -20,7 +21,6 @@ import org.springframework.test.context.jdbc.Sql; |
| 20 | 21 | */ |
| 21 | 22 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) |
| 22 | 23 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 23 | -@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) // 每个单元测试结束前,创建表 | |
| 24 | 24 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 25 | 25 | public class BaseDbUnitTest { |
| 26 | 26 | |
| ... | ... | @@ -30,6 +30,7 @@ public class BaseDbUnitTest { |
| 30 | 30 | DataSourceAutoConfiguration.class, // Spring DB 自动配置类 |
| 31 | 31 | DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 |
| 32 | 32 | DruidDataSourceAutoConfigure.class, // Druid 自动配置类 |
| 33 | + SqlInitializationTestConfiguration.class, // SQL 初始化 | |
| 33 | 34 | // MyBatis 配置类 |
| 34 | 35 | YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 |
| 35 | 36 | MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | ... | ... |
yudao-module-tool/yudao-module-tool-impl/src/test/resources/application-unit-test.yaml