junit使用mock objects进行单元测试
上一篇我介绍了使用stub进行单元测试。那么mock objects和stub有何区别?什么情况下使用mock objects呢?
下面摘自junit in action书中的解释:
mock objects (或者简称为 mocks),非常适用于将某一部分代码与其他代问隔离开来,并对这部分代码进行测试。
mocks 替换了测试中与你的方法协作的对象,从而提供个隔离层。从这一点来讲,它与 stub 有些类似。不过相似之处也仅限于
此,因为mocks 并不实现任何逻辑 它们只是提供了一种使测试能够控制仿造类的所有业务逻辑方法行为的方法的空壳。
下面以一个账户转账的测试用例加以说明。大家可以比较下其与stub的区别。
创建账户类
package com.lhy.junitkaifa.mocks.account;
/**
* 账户
*
* @author xusucheng
* @create 2018-12-22
**/
public class Account {
private String accountId;
private long balance;
public Account(String accountId, long balance){
this.accountId = accountId;
this.balance = balance;
}
public void debit(long amount){
this.balance-=amount;
}
public void credit(long amount){
this.balance+=amount;
}
public long getBalance(){
return this.balance;
}
}
创建配置接口与实现
package com.lhy.junitkaifa.mocks.configurations;
/**
* 配置
*
* @author xusucheng
* @create 2018-12-22
**/
public interface Configuration {
String getSQL(String sqlString);
}
package com.lhy.junitkaifa.mocks.configurations;
/**
* @author xusucheng
* @create 2018-12-22
**/
public class DefaultConfiguration implements Configuration {
public DefaultConfiguration(String configuration){
}
@Override
public String getSQL(String sqlString) {
return null;
}
}
创建账户管理接口与实现
package com.lhy.junitkaifa.mocks.account;
/**
* 账户管理接口
*
* @author xusucheng
* @create 2018-12-22
**/
public interface AccountManager {
Account findAccountById(String userId);
void updateAccount(Account account);
}
package com.lhy.junitkaifa.mocks.account;
import com.lhy.junitkaifa.mocks.configurations.Configuration;
import com.lhy.junitkaifa.mocks.configurations.DefaultConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 账户管理接口实现
*
* @author xusucheng
* @create 2018-12-22
**/
public class AccountManagerImpl implements AccountManager {
private Log logger;
private Configuration configuration;
public AccountManagerImpl(){
this(LogFactory.getLog(AccountManagerImpl.class),new DefaultConfiguration("technical"));
}
public AccountManagerImpl(Log logger, Configuration configuration){
this.logger = logger;
this.configuration = configuration;
}
@Override
public Account findAccountById(String userId) {
this.logger.debug("Getting account for user["+userId+"]");
this.configuration.getSQL("FIND_ACCOUNT_BY_ID");
return null;
}
@Override
public void updateAccount(Account account) {
//Do nothing.
}
}
创建账户管理服务类
package com.lhy.junitkaifa.mocks.account;
/**
* 账户管理服务
*
* @author xusucheng
* @create 2018-12-22
**/
public class AccountService {
private AccountManager accountManager;
/**
* 注入具体实现
* @param accountManager
*/
public void setAccountManager(AccountManager accountManager){
this.accountManager = accountManager;
}
public void transfer(String fromId, String toId, long amount){
//转出账户
Account from = this.accountManager.findAccountById(fromId);
//转入账户
Account to = this.accountManager.findAccountById(toId);
from.debit(amount);
to.credit(amount);
this.accountManager.updateAccount(from);
this.accountManager.updateAccount(to);
}
}
创建测试用账户管理接口
package com.lhy.junitkaifa.mocks.account;
import java.util.HashMap;
import java.util.Map;
/**
* @author xusucheng
* @create 2018-12-22
**/
public class MockAccountManager implements AccountManager{
//保存所有账户
private Map<String,Account> accounts = new HashMap<>();
/**
* 添加账户
* @param userId
* @param account
*/
public void addAccount(String userId, Account account){
this.accounts.put(userId,account);
}
/**
* 获取账户
* @param userId
* @return
*/
@Override
public Account findAccountById(String userId) {
return this.accounts.get(userId);
}
@Override
public void updateAccount(Account account) {
//不干事
}
}
创建测试用日志类
package com.lhy.junitkaifa.mocks.account;
import org.apache.commons.logging.Log;
/**
* @author xusucheng
* @create 2018-12-22
**/
public class MockLog implements Log {
@Override
public boolean isFatalEnabled() {
return false;
}
@Override
public boolean isErrorEnabled() {
return false;
}
@Override
public boolean isWarnEnabled() {
return false;
}
@Override
public boolean isInfoEnabled() {
return false;
}
@Override
public boolean isDebugEnabled() {
return false;
}
@Override
public boolean isTraceEnabled() {
return false;
}
@Override
public void fatal(Object o) {
}
@Override
public void fatal(Object o, Throwable throwable) {
}
@Override
public void error(Object o) {
}
@Override
public void error(Object o, Throwable throwable) {
}
@Override
public void warn(Object o) {
}
@Override
public void warn(Object o, Throwable throwable) {
}
@Override
public void info(Object o) {
}
@Override
public void info(Object o, Throwable throwable) {
}
@Override
public void debug(Object o) {
}
@Override
public void debug(Object o, Throwable throwable) {
}
@Override
public void trace(Object o) {
}
@Override
public void trace(Object o, Throwable throwable) {
}
}
创建测试用配置类
package com.lhy.junitkaifa.mocks.configurations;
/**
* @author xusucheng
* @create 2018-12-22
**/
public class MockConfiguration implements Configuration {
public void setSQL(String sqlString){
}
@Override
public String getSQL(String sqlString) {
return null;
}
}
创建测试类
package com.lhy.junitkaifa.mocks.account;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author xusucheng
* @create 2018-12-22
**/
public class TestAccountService {
@Test
public void testTransferOk(){
MockAccountManager mockAccountManager = new MockAccountManager();
Account from = new Account("1",200);
Account to = new Account("2",100);
mockAccountManager.addAccount("1", from);
mockAccountManager.addAccount("2", to);
AccountService accountService = new AccountService();
accountService.setAccountManager(mockAccountManager);
accountService.transfer("1","2",50);
assertEquals(150,from.getBalance());
assertEquals(150,to.getBalance());
}
}
运行测试
junit使用mock objects进行单元测试的更多相关文章
- 换种思路写Mock,让单元测试更简单
开篇引入 单元测试中的Mock方法,通常是为了绕开那些依赖外部资源或无关功能的方法调用,使得测试重点能够集中在需要验证和保障的代码逻辑上.在定义Mock方法时,开发者真正关心的只有一件事:" ...
- What is the purpose of mock objects?
Since you say you are new to unit testing and asked for mock objects in "layman's terms", ...
- 学习笔记:首次进行JUnit+Ant构建自动的单元测试(二)
关键字:JUnit,Ant,单元测试 终于把JUnit+Ant构建单元测试的大概了解了,其实我实践的过程是对了,只是指导博客(看到这里不懂请看我上一篇博客)本身的错误“成功”把我带入“坑”,有时候网友 ...
- 学习笔记:首次进行JUnit+Ant构建自动的单元测试(一)
指导博客:https://blog.csdn.net/Cceking/article/details/51692010 基于软件测试的需求,使用JUnit+Ant构建自动的单元测试. IDE:ecli ...
- 有效使用Mock编写java单元测试
Java单元测试对于开发人员质量保证至关重要,尤其当面对一团乱码的遗留代码时,没有高覆盖率的单元测试做保障,没人敢轻易对代码进行重构.然而单元测试的编写也不是一件容易的事情,除非使用TDD方式,否则编 ...
- 使用junit进行最简单的单元测试
使用junit进行最简单的单元测试 使用工具: jdk IDEA Maven 第一步 创建一个Maven项目 第二步 导入junit依赖 <dependency> <groupId& ...
- 【java测试】Junit、Mock+代码覆盖率
原文见此处 单元测试是编写测试代码,用来检测特定的.明确的.细颗粒的功能.单元测试并不一定保证程序功能是正确的,更不保证整体业务是准备的. 单元测试不仅仅用来保证当前代码的正确性,更重要的是用来保证代 ...
- 使用 Python Mock 类进行单元测试
数据类型.模型或节点——这些都只是mock对象可承担的角色.但mock在单元测试中扮演一个什么角色呢? 有时,你需要为单元测试的初始设置准备一些“其他”的代码资源.但这些资源兴许会不可用,不稳定,或者 ...
- 使用Python中的mock模块进行单元测试
在进行单元测试的时候,有时候会遇到这种情况: 出于某些原因,我们不想测试某一部分内容,但是我们想要测试的部分却依赖这部分内容. 这时候,可以使用mock模块来模拟调用这部分内容,并给出返回结果,举例如 ...
- JUNIT -- springMVC的action进行单元测试
原文:http://blog.csdn.net/gaopeng0071/article/details/49946575 我开发环境springMVC版本3.0.4 样例代码: package com ...
随机推荐
- [转帖]MySQL 8.0 以后的版本策略变化
https://www.modb.pro/db/1717815842220630016 产品版本变更 从2023年7月18日开始,MySQL官网出现了一个新的版本 MySQL 8.1.0,直接改变 ...
- [粘贴]Introducing Exadata X9M: Dramatically Faster, More Cost Effective, and Easier to Use
https://blogs.oracle.com/exadata/post/exadata-x9m The Exadata Product Management and Development t ...
- [转帖]麒麟v10上部署TiDBv5.1.2生产环境的最佳实践
https://tidb.net/book/tidb-monthly/2022/2022-07/usercase/tidb-v5-1-2 前言 笔者最近在一个银行项目中做 PoC 测试,由于客户选择 ...
- [转帖]Linux开源存储漫谈(2)IO性能测试利器fio
fio(Flexible I/O Tester)正是非常常用的文件系统和磁盘 I/O 性能基准测试工具.提供了大量的可定制化选项,可以用来测试,裸盘.一个单独的分区或者文件系统在各种场景下的 I/O ...
- [转帖]docker 最新版本升级
文章目录 前言 一.卸载低版本docker 1.1 检查docker版本 1.2 删除docker 二.开始安装 2.1 安装所需依赖 2.2 设置docker yum源 2.3 查看所有可用版本 2 ...
- ebpf的简单学习
ebpf的简单学习-万事开头难 前言 bpf 值得是巴克利包过滤器 他的核心思想是在内核态增加一个可编程的虚拟机. 可以在用户态定义很多规则, 然后直接在内核态进行过滤和使用. 他的效率极高. 因为避 ...
- [粘贴]【CPU】关于x86、x86_64/x64、amd64和arm64/aarch64
[CPU]关于x86.x86_64/x64.amd64和arm64/aarch64 https://www.jianshu.com/p/2753c45af9bf 为什么叫x86和x86_64和AMD6 ...
- 华为云CCE Turbo:基于eBPF的用户自定义多粒度网络监控能力
本文分享自华为云社区<华为云CCE Turbo:基于eBPF的用户自定义多粒度网络监控能力>,作者: 云容器大未来. 基于eBPF的容器监控的兴起 容器具有极致弹性.标准运行时.易于部署等 ...
- TienChin 渠道管理-渠道导出
ChannelController /** * 导出渠道列表 */ @PreAuthorize("hasPermission('tienchin:channel:export')" ...
- batch size设置技巧
1.什么是BatchSize Batch一般被翻译为批量,设置batch_size的目的让模型在训练过程中每次选择批量的数据来进行处理.Batch Size的直观理解就是一次训练所选取的样本数. Ba ...
