一个简单的使用mock技术进行测试的例子
Account:
public class Account { private String acountId; private long balance; public Account(String acountId, long balance) { this.acountId = acountId; this.balance = balance; } public void debit(long amount) { this.balance-=amount; } public void credit(long amount) { this.balance+=amount; } public long getBalance() { return balance; } }
AccountManager:
public interface AccountManager { Account findAccountForUser(String userId); void updateAccount(Account account); }
AccountService:
public class AccountService { private AccountManager accountManager; public void setAccountManager(AccountManager accountManager) { this.accountManager = accountManager; } public void transfer(String senderId, String befeficiaryId, long amount) { Account sender = this.accountManager.findAccountForUser(senderId); Account beneficiary = this.accountManager.findAccountForUser(befeficiaryId); sender.debit(amount); beneficiary.credit(amount); this.accountManager.updateAccount(sender); this.accountManager.updateAccount(beneficiary); } }
TestAccountService:
import org.junit.Test; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; public class TestAccountService { public class MockAccountManager implements AccountManager { private Map<String,Account> accounts = new HashMap<String,Account>(); public void addAccount(String userId, Account account) { this.accounts.put(userId, account); } public Account findAccountForUser(String userId) { return this.accounts.get(userId); } public void updateAccount(Account account) { //do nothing... } } @Test public void testTransferOk(){ MockAccountManager mockAccountManager = new MockAccountManager(); Account senderAccount = new Account("1", 200); Account deneficiaryAccount = new Account("1", 100); mockAccountManager.addAccount("1", senderAccount); mockAccountManager.addAccount("2", deneficiaryAccount); AccountService accountService = new AccountService(); accountService.setAccountManager(mockAccountManager); accountService.transfer("1","2",50); assertEquals(150,senderAccount.getBalance()); assertEquals(150,deneficiaryAccount.getBalance()); } }
一个简单的使用mock技术进行测试的例子的更多相关文章
- 《maven实战》笔记(2)----一个简单maven项目的搭建,测试和打包
参照<maven实战>在本地创建对应的基本项目helloworld,在本地完成后项目结构如下: 可以看到maven项目的骨架:src/main/java(javaz主代码)src/test ...
- 一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子
一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子 最近,由于云计算的发展,数据库技术也从结构式数据库发展到NoSQL数据库,存储模式从结构化的关系存储到现在如火如荼的key/val ...
- Python实现一个简单三层神经网络的搭建并测试
python实现一个简单三层神经网络的搭建(有代码) 废话不多说了,直接步入正题,一个完整的神经网络一般由三层构成:输入层,隐藏层(可以有多层)和输出层.本文所构建的神经网络隐藏层只有一层.一个神经网 ...
- 大话重构连载15:采用Mock技术完成测试
第五次重构我们引入了数据库的设计,用户信息要从数据库中读取,问候语库存储在数据库中,并支持添加与更新.数据库的引入使自动化测试变得困难了,因为数据状态总是变化着的,而这种变化使得测试过程不能复现,这是 ...
- Criterion - 一个简单可扩展的 C 语言测试框架
A dead-simple, yet extensible, C test framework. Philosophy Most test frameworks for C require a lot ...
- 一个简单的C#+arcgis的非数据库版例子
1.首先新建一个winform的项目. 2.确保C#工具箱包含ESRI的相关控件,如果没有就需要安装SDK. 如果VS中依旧不存在esri控件解决方案如下,以VS2013为例: (1)打开注册表,定位 ...
- 一个简单的div弹出层的小例子
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Jmeter初步使用二--使用jmeter做一个简单的性能测试
经过上一次的初步使用,我们懂得了Jmeter的安装与初步使用的方法.现在,我们使用Jmeter做一个简单的性能测试.该次测试,提交的参数不做参数化处理,Jmeter各元件使用将在介绍在下一博文开始介绍 ...
- 一个简单的基于 DirectShow 的播放器 1(封装类)
DirectShow最主要的功能就是播放视频,在这里介绍一个简单的基于DirectShow的播放器的例子,是用MFC做的,今后有机会可以基于该播放器开发更复杂的播放器软件. 注:该例子取自于<D ...
随机推荐
- 十七 bootstrap-table tableExport 导出xlsx格式表格
原文:十七 bootstrap-table tableExport 导出xlsx格式表格 在[十六.bootstrap-table javascript导出数据]中,打开导出的表格时,总会弹出一个提示 ...
- CROSS JOIN
原文:CROSS JOIN 最近在讲到T-SQL查询的Join部分时,一下子没有想起来CROSS JOIN的用法,因为其实平常也确实基本不用到.特意找了一个例子,以供参考 CROSS JOIN又称为笛 ...
- Failed to recover corrupt cache entry
RangeError java.lang.RuntimeException: ERROR: Failed to recover corrupt cache entry at com.sun.deplo ...
- C#正则表达式简单案例解析
正则表达式主要用于字符串的操作. 1.Regex.IsMatch:判断指定的字符串是否符合正则表达式. 2.Regex.Match:提取匹配的字符串,只能提取到第一个符合的字符串.这里还可以使用组来提 ...
- MacOS X编译OpenSceneGraph
本文主要记录在MacOS X上编译OpenSceneGraph,方便日后查阅.所使用的环境如下: MacOS X 10.10 Yosemite XCode 6.3.2 CMake 3.3.0 Open ...
- inno setup 配合istools
istool,可以认为是inno setup的IDE吧 用用就知道了,istool用起来方便些,当然,脚本写好了,用不用istool就无所谓了
- vue+TS(CLI3)
1.用CLI3创建项目 查看当前CLI的版本,如果没有安装CLI3的 使用npm install --global vue-cli来安装CLI 安装好CLI 可以创建项目了 使用vue create ...
- 【Web前端Talk】无聊吗?写个【飞机大战】来玩吧(上篇)
01前言介绍 微信小游戏是基于微信客户端的游戏,它即点即玩,无需下载安装,体验轻便,可以和微信内的好友一起玩,比如PK.围观等,享受小游戏带来的乐趣.那如何开发一款属于自己的小游戏呢? 源码地址: h ...
- SYN2306型 北斗串口时间服务器
SYN2306型 北斗串口时间服务器 北斗授时设备北斗时钟同步系统使用说明视频链接: http://www.syn029.com/h-pd-108-0_310_36_-1.html 请将此链接复制 ...
- netty服务端启动--ServerBootstrap源码解析
netty服务端启动--ServerBootstrap源码解析 前面的第一篇文章中,我以spark中的netty客户端的创建为切入点,分析了netty的客户端引导类Bootstrap的参数设置以及启动 ...