/单元测试

承接上文

统一异常处理,封装结果-CSDN博客

********************************************

登录业务

java 复制代码
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private JwtUtils jwtUtils;
    @Override
    public String login(Employee employee) {
        //2.构造条件
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername())
                .or()
                .eq(Employee::getPhone,employee.getPhone());
        //1. 根据用户名或者密码查询用户信息
        Employee employeeInDb = this.getOne(wrapper);

        //3.判断是否查询到用户
        if(employeeInDb == null){
            throw new BusinessException(ResponseEnum.USER_INVALID);
        }

        //4.比较密码
        if(!BCrypt.checkpw(employee.getPassword(),employeeInDb.getPassword())){
            throw new BusinessException(ResponseEnum.PASSWORD_INVALID);
        }
        //5.签发token

        String token = jwtUtils.createJwt(
                employeeInDb.getId(),
                null,
                60*60*24*7L,
                "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        );
        return token;
    }
}

现要对这个登录业务进行测试,要测试的情况有 登录成功,账号或电话输入错误,密码输入错误三种情况

异常状态码

单元测试

通过断言对失败的情况进行判定,通过Assertions.assertEquals进行进一步判定异常的具体出错信息。

java 复制代码
@SpringBootTest
public class EmployeeControllerTests {
    @Resource
    private EmployeeController employeeController;

    @Test
    //成功的情况
    public void testLoginSuccess() {
        Employee employee = new Employee();
        employee.setUsername("admin");
        employee.setPassword("123456");
    }

    @Test
    public void testLoginPasswordError() {
        //断言
        BusinessException e = Assertions.assertThrows(BusinessException.class, () -> {
            Employee employee = new Employee();
            employee.setUsername("admin");
            employee.setPassword("1234567");
            employeeController.login(employee);
        });
        //进一步断定更详细的异常类型
        Assertions.assertEquals("密码错误",e.getResponse().getMessage());
    }

    @Test
    public void testLoginUsernameError() {
        BusinessException e = Assertions.assertThrows(BusinessException.class, () -> {
            Employee employee = new Employee();
            employee.setUsername("adminaa");
            employee.setPassword("123456");
            employeeController.login(employee);
        });
        Assertions.assertEquals("用户不存在",e.getResponse().getMessage());
    }

}

测试结果

相关推荐
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹4 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭4 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫4 小时前
泛型(2)
java
超爱吃士力架5 小时前
邀请逻辑
java·linux·后端
南宫生5 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石5 小时前
12/21java基础
java
李小白665 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp5 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶6 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb