mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
开始编写 SysAuthServiceImpl 的单元测试
This commit is contained in:
parent
e9f2e91f7e
commit
92997a0cd5
@ -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 {
|
||||
|
||||
/**
|
||||
|
@ -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 {
|
||||
|
@ -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 服务器
|
||||
*/
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.dashboard.modules.system.service;
|
66
src/test/java/cn/iocoder/dashboard/util/RandomUtils.java
Normal file
66
src/test/java/cn/iocoder/dashboard/util/RandomUtils.java
Normal file
@ -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> T randomObject(Class<T> 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user