Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试
由于注册时,需要对输入的密码进行加密,使用到了 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 单元测试的更多相关文章
- Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock
在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...
- Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性
在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...
- Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题
按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...
- Spring Boot 2 实践记录之 Powermock 和 SpringBootTest
由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...
- Spring Boot 2 实践记录之 Redis 及 Session Redis 配置
先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...
- Spring Boot 2 实践记录之 组合注解原理
Spring 的组合注解功能,网上有很多文章介绍,不过都是介绍其使用方法,鲜有其原理解析. 组合注解并非 Java 的原生能力.就是说,想通过用「注解A」来注解「注解B」,再用「注解B」 来注解 C( ...
- Spring Boot 2 实践记录之 MySQL + MyBatis 配置
如果不需要连接池,那么只需要简单的在pom文件中,添加mysql依赖: <dependency> <groupId>mysql</groupId> <arti ...
- Spring Boot 2 实践记录之 条件装配
实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...
- Spring Boot 之日志记录
Spring Boot 之日志记录 Spring Boot 支持集成 Java 世界主流的日志库. 如果对于 Java 日志库不熟悉,可以参考:细说 Java 主流日志工具库 关键词: log4j, ...
随机推荐
- 基于Woodstox的StAX 2 (Streaming API for XML)解析XML
StAX (Streaming API for XML)面向流的拉式解析XML,速度快.占用资源少,非常合适处理大数据量的xml文件. 详细教程和说明可以参见以下几篇文章: 使用 StAX 解析 XM ...
- Mybatis中的N+1问题与延迟加载
0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...
- Telephone interview with Youyou Tu
"Good News for the National Holiday!" Telephone interview with Youyou Tu following the ann ...
- reduction
reduction - 必应词典 美[rɪ'dʌkʃən]英[rɪ'dʌkʃ(ə)n] n.还原:降低:减少:缩小 网络缩减:减量:减小 变形复数:reductions:
- Spring框架的AOP技术之通知类型
1. 通知类型 * @Before -- 前置通知 * @AfterReturing -- 后置通知 * @Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行) * @After - ...
- 泛型约束where条件的使用(可以通过类型参数动态反射创建实例)
定义抽象的人类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 安卓编译 translate error Lint: How to ignore “<key> is not translated in <language>” errors?
Add following at the header of your strings.xml file <resources xmlns:tools="http://schemas. ...
- at java.net.InetAddress.getLocalHost(InetAddress.java:1475)
今天在centos 安装hadoop安装完成后执行wordcount的时候报如下错误: at java.net.InetAddress.getLocalHost(InetAddress.java:14 ...
- ubuntu 'yuan' update
# tsinghua university deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiv ...
- 2018.09.08 bzoj4518: [Sdoi2016]征途(斜率优化dp)
传送门 把式子展开后发现就是要求: m∗(∑i=1msum′[i])−sum[n]2" role="presentation" style="position: ...