由于注册时,需要对输入的密码进行加密,使用到了 UUID、sha1、md 等算法。在单元测试时,使用到了 Powermock,记录如下。

先看下加密算法:

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils; import java.util.UUID; public class Encrypt {
/**
* 密码加密
* @param password 待加密的密码
* @param md5 是否先用 md5 加密
* @return 加密后的密码
*/
public static String passwordGenerator(String password) {
password = DigestUtils.md5Hex(password);
String salt = DigestUtils.sha1Hex(UUID.randomUUID().toString()).substring(0, 4);
String saltString = DigestUtils.sha1Hex(password + salt) + salt;
String encryPassword = Base64.encodeBase64String(saltString.getBytes());
return encryPassword;
}
}

其中,UUID.randomUUID()、DigestUtils.md5Hex()、DigestUtils.sha1Hex()、Base64.encodeBase64String() 均为静态方法,而 uuid.toString() 则为 UUID 实例对象的方法。

对于 UUID 的 mock,需要两步:

第一步,是使用 mockito mock 一个 UUID 对象,并 mock 其在代码中使用的方法,这里要 mock 的是 toString() 方法。

第二步,是使用 powormockito,mock UUID 的 randomUUID() 方法,使其返回上一步 mock 的 uuid 对象,这样我们就能得到预期的 uuid 的方法(这里是 toString)的执行结果。

DigestUtils 和 Base64 的静态方法直接使用 powermockito 来 mock 就可以了。

具体步骤如下:

一、添加依赖包(使用 maven):

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>

二、在测试类上添加 @Runwith 和 @PrepareForTest 注解

三、在测试方法中对类和对象进行 mock。

示例代码:

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.apache.commons.codec.binary.Base64;
import java.util.UUID; import static org.junit.Assert.*; @RunWith(PowerMockRunner.class)
@PrepareForTest({UUID.class, DigestUtils.class, Encrypt.class, Base64.class})
public class EncryptTest { @Test
public void passwordGeneratorWithMd5() {
String randomString = "mbaefaeq";
String salt = "abcd";
String password = "123456";
String rePassword = "654321";
String twiceSaltMd5 = "hellomd5"; // mock uuid 对象,使其 toString() 方法返回预定义的 randomString 字符串
UUID uuid = PowerMockito.mock(UUID.class);
Mockito.when(uuid.toString()).thenReturn(randomString); // mock UUID 类,使其 randomUUID() 方法返回刚刚 mock 的 uuid 对象
PowerMockito.mockStatic(UUID.class);
PowerMockito.when(UUID.randomUUID()).thenReturn(uuid); // mock DigestUtils 类,使其 sha1Hex() 方法在接收预定义的 randomString 参数时,返回预定义的 salt 字符串
PowerMockito.mockStatic(DigestUtils.class);
PowerMockito.when(DigestUtils.sha1Hex(randomString)).thenReturn(salt); // 使 mock 的 DigestUtils 类的 md5Hex 方法,在接受预定义的 password 时,生成预定义的 rePassword 字符串
PowerMockito.when(DigestUtils.md5Hex(password)).thenReturn(rePassword); // 使 mock 的 DigestUtils 类的 sha1Hex() 方法在接收预定义的 rePassword 和 salt 时,返回 预定义的 twiceSaltMd5 字符串
PowerMockito.when(DigestUtils.sha1Hex(rePassword + salt)).thenReturn(twiceSaltMd5); // mock Base64 类,使其encodeBase64String() 方法在接收 预定义的串时,返回预定义的加密后密码
PowerMockito.mockStatic(Base64.class);
String imencryptpassword = "imencryptpasswordwithmd5";
PowerMockito.when(Base64.encodeBase64String((twiceSaltMd5 + salt).getBytes())).thenReturn(imencryptpassword); // 调用加密方法,并验证结果
String encryptPassword = Encrypt.passwordGenerator("123456");
assertEquals(imencryptpassword, encryptPassword);
}
}

Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试的更多相关文章

  1. Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock

    在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...

  2. Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性

    在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...

  3. Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题

    按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...

  4. Spring Boot 2 实践记录之 Powermock 和 SpringBootTest

    由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...

  5. Spring Boot 2 实践记录之 Redis 及 Session Redis 配置

    先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...

  6. Spring Boot 2 实践记录之 组合注解原理

    Spring 的组合注解功能,网上有很多文章介绍,不过都是介绍其使用方法,鲜有其原理解析. 组合注解并非 Java 的原生能力.就是说,想通过用「注解A」来注解「注解B」,再用「注解B」 来注解 C( ...

  7. Spring Boot 2 实践记录之 MySQL + MyBatis 配置

    如果不需要连接池,那么只需要简单的在pom文件中,添加mysql依赖: <dependency> <groupId>mysql</groupId> <arti ...

  8. Spring Boot 2 实践记录之 条件装配

    实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...

  9. Spring Boot 之日志记录

    Spring Boot 之日志记录 Spring Boot 支持集成 Java 世界主流的日志库. 如果对于 Java 日志库不熟悉,可以参考:细说 Java 主流日志工具库 关键词: log4j, ...

随机推荐

  1. css:多个div在同一行显示

    使用float:left,也可以使用display : inline-block,可以使多个div在同一行显示. 示例如下: <div class="search_row"& ...

  2. org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException

    使用Intelij Idea时,报错如下: org.apache.catalina.core.StandardContext.listenerStart Error configuring appli ...

  3. 快速上手Vue

    课程目录: ES6常用语法 Vue基础以及指令 Vue组件 Vue-Router Vue生命周期 npm webpack vue-cli Vuex以及axios

  4. Leapin' Lizards(经典建图,最大流)

    Leapin' Lizards http://acm.hdu.edu.cn/showproblem.php?pid=2732 Time Limit: 2000/1000 MS (Java/Others ...

  5. .NET中的文件IO操作实例

    1.写入文件代码: //1.1 生成文件名和设置文件物理路径 Random random = new Random(DateTime.Now.Millisecond); ); string Physi ...

  6. 利用python计算windows全盘文件md5值的脚本

    import hashlib import os import time import configparser import uuid def test_file_md5(file_path): t ...

  7. centos自定义安装pip3

    题记 在之前的文章centos云服务器安装Python3记录 记录了怎么自定义安装 Python3 ,在后边测试pip3的时候发现了个问题: pip --version terminal 打印: pi ...

  8. fusioncharts Y轴不显示中文的解决方法(转载)

    使用fusionChart主要是被其界面吸引了,各类图表都很好看,下载以后文档也很周全,支持的语言也很多种 ,容易上手.fusionChart工作原理主要是通过后台传xml数据源给报表前台flash ...

  9. hadoop应用场景总结

    原文地址 我个人接触hadoop仅仅不到一年,因为是业余时间学习,故进度较慢,看过好多视频,买过好多书,学过基本知识,搭建过伪分布式集群,有过简单的教程式开发,恰逢毕业季,面试过相关岗位,自认为路还很 ...

  10. pip常用操作指令

    1.安装模块 pip install vitualenv pip install -r requirement.txt 2.查询模块信息 pip show pip 3.显示已经安装的模块 pip li ...