From 92997a0cd57d0c9bd5d77ab3ff165501c47ed945 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 2 Mar 2021 00:58:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E7=BC=96=E5=86=99=20SysAuthS?= =?UTF-8?q?erviceImpl=20=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/dal/dataobject/user/SysUserDO.java | 6 +- .../dashboard/BaseSpringBootUnitTest.java | 2 +- .../config/RedisTestConfiguration.java | 8 --- .../service/auth/SysAuthServiceImplTest.java | 33 ++++++++++ .../modules/system/service/package-info.java | 1 + .../iocoder/dashboard/util/RandomUtils.java | 66 +++++++++++++++++++ 6 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java create mode 100644 src/test/java/cn/iocoder/dashboard/modules/system/service/package-info.java create mode 100644 src/test/java/cn/iocoder/dashboard/util/RandomUtils.java diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/user/SysUserDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/user/SysUserDO.java index 6561fbd5f..6eab991c1 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/user/SysUserDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/user/SysUserDO.java @@ -7,8 +7,7 @@ import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import lombok.Data; -import lombok.EqualsAndHashCode; +import lombok.*; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import java.util.Date; @@ -22,6 +21,9 @@ import java.util.Set; @TableName(value = "sys_user", autoResultMap = true) @Data @EqualsAndHashCode(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor public class SysUserDO extends BaseDO { /** diff --git a/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java b/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java index fa75d3533..a00fc472d 100644 --- a/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java +++ b/src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java @@ -9,7 +9,7 @@ import org.springframework.test.context.jdbc.Sql; import javax.annotation.Resource; -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB public class BaseSpringBootUnitTest { diff --git a/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java b/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java index 18b609f8e..1cddd9e1c 100644 --- a/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java +++ b/src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java @@ -18,14 +18,6 @@ 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 服务器 */ diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java new file mode 100644 index 000000000..a5c9fde0b --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java @@ -0,0 +1,33 @@ +package cn.iocoder.dashboard.modules.system.service.auth; + +import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; +import cn.iocoder.dashboard.modules.system.service.auth.impl.SysAuthServiceImpl; +import cn.iocoder.dashboard.modules.system.service.user.SysUserService; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; + +import javax.annotation.Resource; + +import static cn.iocoder.dashboard.util.RandomUtils.*; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { + + @Resource + private SysAuthServiceImpl authService; + + @MockBean + private SysUserService userService; + + @Test + public void testLoadUserByUsername_success() { + // 准备参数 + String username = randomString(); + // mock 方法 + SysUserDO user = randomUserDO(); + when(userService.getUserByUserName(eq(username))).thenReturn(user); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/package-info.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/package-info.java new file mode 100644 index 000000000..09c1d9d14 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.dashboard.modules.system.service; diff --git a/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java b/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java new file mode 100644 index 000000000..88a96ce76 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java @@ -0,0 +1,66 @@ +package cn.iocoder.dashboard.util; + +import cn.hutool.core.util.*; +import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; + +import java.lang.reflect.Field; + +/** + * 随机工具类 + * + * @author 芋道源码 + */ +public class RandomUtils { + + private static final int RANDOM_STRING_LENGTH = 10; + + public static String randomString() { + return RandomUtil.randomString(RANDOM_STRING_LENGTH); + } + + public static Long randomLong() { + return RandomUtil.randomLong(0, Long.MAX_VALUE); + } + + public static Integer randomInteger() { + return RandomUtil.randomInt(0, Integer.MAX_VALUE); + } + + public static Short randomShort() { + return (short) RandomUtil.randomInt(0, Short.MAX_VALUE); + } + + public static SysUserDO randomUserDO() { + SysUserDO user = randomObject(SysUserDO.class); + return user; + } + + private static T randomObject(Class clazz) { + // 创建对象 + T object = ReflectUtil.newInstance(clazz); + // 遍历属性,设置随机值 + for (Field field : ReflectUtil.getFields(clazz)) { + // 数字类型 + if (field.getType() == Long.class) { + ReflectUtil.setFieldValue(object, field, randomLong()); + continue; + } + if (field.getType() == Integer.class) { + ReflectUtil.setFieldValue(object, field, randomInteger()); + continue; + } + if (field.getType() == Short.class) { + ReflectUtil.setFieldValue(object, field, randomShort()); + continue; + } + // 字符串类型 + if (field.getType() == String.class) { + ReflectUtil.setFieldValue(object, field, randomString()); + continue; + } +// System.out.println(); + } + return object; + } + +}