- 浏览: 940489 次
- 性别:
- 来自: 江西上饶
文章分类
- 全部博客 (460)
- p.spring (56)
- p.maven (20)
- p.ant (17)
- p.jee (18)
- p.jse (33)
- p.ofbiz (31)
- p.软件工程 (8)
- p.struts2 (5)
- p.hibernate (5)
- linux (25)
- 设计模式 (2)
- p.javascript (11)
- 硬件 (1)
- p.jsp (2)
- p.windows批处理 (1)
- 操作系统问题 (5)
- 算法 (1)
- p.mysql (7)
- p.sql (5)
- p.c (1)
- google产品 (0)
- 内存 (1)
- p.struts (1)
- p.freemarker (7)
- p.css (4)
- p.log4j (10)
- p.html (3)
- 淘宝产品 (0)
- 其他 (3)
- 编译器 (0)
- svn (4)
- p.spring.security (11)
- 图形 (0)
- p.xml (1)
- p.ssh (0)
- p.jquery (4)
- p.jdbc (3)
- p.flex (0)
- p.c++ (0)
- p.c#Net (0)
- p.assembly (0)
- p.sqlserver (0)
- p.其他 (3)
- p.webwork (21)
- p.wap (12)
- p.cglib (1)
- p.jee服务器 (11)
- windows (2)
- p.iphone (1)
- p.java.分布式与集群 (2)
- p.ibatis (16)
- p.eclipse (5)
- 架构 (2)
- http协议 (5)
- 我的个人标准 (2)
- 多线程 (1)
- 奇怪问题 (5)
- p.jira (13)
- p.httpclient (1)
- 服务器.apache (11)
- 安全防范 (1)
- p.PODAM (1)
- p.junit (16)
- fop (2)
- 硬盘安装 (1)
- powerdesigner (0)
- 单元测试 (1)
- apache commons (4)
- tomcat+apache集群 (10)
- 各类诡辩 (1)
- 安卓 (8)
- qvod (1)
- java编程基础知识考试考点及答案 (0)
- 工作总结 (4)
- oracle (0)
- spring的util工具 (3)
- json (2)
- maven (3)
- jms (19)
- p.bat (3)
- hadoop (2)
- git (3)
- nginx (1)
- p.移动开发 (1)
- shiro (3)
- 游戏破解 (1)
- react-native (7)
- ios开发 (1)
- webmagic (6)
- socks5 (1)
最新评论
-
weituotian:
说的不好,没人看的
公司系统中的菜单功能和权限功能 -
石不易:
非常详细的注解~
绑定端口和IP,Listen 与VirtualHost指令 -
spring_springmvc:
spring mvc demo教程源代码下载,地址:http: ...
spring mvc -
liyixing1:
PandaDONG 写道谢谢你啊,我已经下下来了,只是还有很多 ...
jira war安装 -
liyixing1:
PandaDONG 写道谢谢你啊,我已经下下来了,只是还有很多 ...
jira war安装
package mock;
public class Account {
private String accountId;
private long balance;
public Account(String accountId, long initialBalance) {
this.accountId = accountId;
this.balance = initialBalance;
}
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public void setBalance(long balance) {
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 mock;
public interface AccountManager {
public Account findAccountForUser(String userId);
public void updateAccount(Account account);
}
package mock;
public class AccountService {
private AccountManager accountManager;
public void setAccountManager(AccountManager manager) {
this.accountManager = manager;
}
public void transfer(String senderId, String beneficiaryId, long amount) {
Account sender = this.accountManager.findAccountForUser(senderId);
Account beneficiary = this.accountManager
.findAccountForUser(beneficiaryId);
sender.debit(amount);
beneficiary.credit(amount);
this.accountManager.updateAccount(sender);
this.accountManager.updateAccount(beneficiary);
}
}
为了而是transfer,所以必须要有一个数据访问层对象,而我们还没完成这个类的编写,这个时候就mock一个数据访问层对象。
package mock;
import java.util.HashMap;
import java.util.Map;
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) {
}
}
package mock;
import org.junit.Assert;
import org.junit.Test;
public class TestAccountService {
@Test
public void testTransferOk() {
MockAccountManager mockAccountManager = new MockAccountManager();
Account senderAccount = new Account("1", 200);
Account beneficiaryAccount = new Account("2", 100);
mockAccountManager.addAccount("1", senderAccount);
mockAccountManager.addAccount("2", beneficiaryAccount);
AccountService accountService = new AccountService();
accountService.setAccountManager(mockAccountManager);
accountService.transfer("1", "2", 50);
Assert.assertEquals(150, senderAccount.getBalance());
Assert.assertEquals(150, beneficiaryAccount.getBalance());
}
}
//当然这里更好的做法是mock的数据从外部文件或者xml等读入。
EasyMock是一个mock框架。
public class Account {
private String accountId;
private long balance;
public Account(String accountId, long initialBalance) {
this.accountId = accountId;
this.balance = initialBalance;
}
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public void setBalance(long balance) {
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 mock;
public interface AccountManager {
public Account findAccountForUser(String userId);
public void updateAccount(Account account);
}
package mock;
public class AccountService {
private AccountManager accountManager;
public void setAccountManager(AccountManager manager) {
this.accountManager = manager;
}
public void transfer(String senderId, String beneficiaryId, long amount) {
Account sender = this.accountManager.findAccountForUser(senderId);
Account beneficiary = this.accountManager
.findAccountForUser(beneficiaryId);
sender.debit(amount);
beneficiary.credit(amount);
this.accountManager.updateAccount(sender);
this.accountManager.updateAccount(beneficiary);
}
}
为了而是transfer,所以必须要有一个数据访问层对象,而我们还没完成这个类的编写,这个时候就mock一个数据访问层对象。
package mock;
import java.util.HashMap;
import java.util.Map;
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) {
}
}
package mock;
import org.junit.Assert;
import org.junit.Test;
public class TestAccountService {
@Test
public void testTransferOk() {
MockAccountManager mockAccountManager = new MockAccountManager();
Account senderAccount = new Account("1", 200);
Account beneficiaryAccount = new Account("2", 100);
mockAccountManager.addAccount("1", senderAccount);
mockAccountManager.addAccount("2", beneficiaryAccount);
AccountService accountService = new AccountService();
accountService.setAccountManager(mockAccountManager);
accountService.transfer("1", "2", 50);
Assert.assertEquals(150, senderAccount.getBalance());
Assert.assertEquals(150, beneficiaryAccount.getBalance());
}
}
//当然这里更好的做法是mock的数据从外部文件或者xml等读入。
EasyMock是一个mock框架。
发表评论
-
DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-10-20 10:28 27594在单元测试测试环境下主要参数两个错误信息: 1.java.la ... -
奇怪的乱码
2011-10-20 00:14 1337通过ant运行build,然后启动测试。测试的时候有log4j ... -
依赖外部资源时解决方案-----存根
2011-10-17 22:08 1438package stubbing; import java. ... -
cobertura做测试覆盖率 ant的配置
2011-09-25 23:05 7410cobertura原理大致如下: 首先修改我们编译后的clas ... -
软件测试的种类
2011-09-18 17:58 1122acceptance test 验收测试 你的程序满足了客户的 ... -
hamcrest
2011-09-18 16:51 1175在一个测试用例中,有时候不得不存在多个断言,比如 @Test ... -
@Ignore 指定跳过测试用例
2011-09-18 16:06 1718在3.x版本中,如果我们不想启动某些测试,需要将方法名更改为非 ... -
@Test
2011-09-17 23:27 8070@Test注解是测试的基础,它提供了其他作用 1.指定将会抛 ... -
测试规范
2011-09-16 00:27 975测试方法名一般有两种 testxxxx xxxx是要测试的域 ... -
@Before, @BeforeClass, @After, @AfterClass
2011-09-15 23:30 7790@Before, @BeforeClass, @After, ... -
Suite runner
2011-09-14 23:29 1120套件运行器 它可以运行多个测试。 package test; ... -
@Parameters Parameterized runner
2011-09-14 22:46 1591先看一个例子 package te ... -
junit的核心对象
2011-09-12 21:52 1148概念责任Assert不同条件的断言。如果成功,无什么特殊的信息 ... -
基础知识
2011-09-12 20:01 900注解 @Before, @BeforeClass, @Afte ... -
junit网址
2011-08-31 16:29 982http://junit.sourceforge.net/ h ...
相关推荐
Mock模拟测试,定义、好处、关键点、Mockito使用、Mock 对象的行为验证、Spy 对象验证等
Mock counterfeiter - 一个工具用于在Go中生成自容器,类型安全的测试Mock对象
结论下载参考资料本文主要讨论如何利用配置文件对Mock对象以及它的行为进行描述,从而分离测试数据和代码,创建高效、灵活的测试用例。同时,本文给出了一套基于开源项目EasyMock的实现,并通过一个示例来说明如何...
在java web应用中,进行用例实现时,很多情况难以模拟...它提供对接口的模拟,能够通过录制、回放、检查三步来完成大体的测试过程,可以验证方法的调用种类、次数、顺序,可以令 Mock 对象返回指定的值或抛出指定异常。
它也是属于使用Mock对象测试,但是与EasyMock不同的是,EasyMock是提供了创建Mock对象的API,而StrutsTest则是专门负责测试Struts应用程序的Mock对象测试框架。除了容器外测试,StrutsTest也可以很方便的用容器内的...
EasyMock 是一套通过简单的方法对于指定的接口或类生成 Mock 对象的类库,它能利用对接口或类的模拟来辅助单元测试。 Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较...
在开发过程中往往需要配合单元测试,但是很多时候,单元测试需要依赖...mock对象就是为了解决上面的问题而诞生的,mock(模拟)对象能够模拟实际依赖对象的功能,同时又不需要非常复杂的准备工作,你需要做的,仅仅就是
其主要功能是创建和配置Mock对象,以简化存在外部依赖的类的测试。通过使用Mockito,我们可以虚拟出一个外部依赖,这样可以降低测试组件之间的耦合度,使得我们能更专注于代码的流程和结果,从而更好地实现测试目的...
不错的文档,提供给科研人员
Python mock的基本使用介绍。Mock即模拟的意思。在Python中,提供了基于单元测试的Mock模块,它的主要作用是使用mock对象替代掉指定的Python对象,以达到模拟对象功能的行为。
EasyMock 是一套通过简单的方法对于指定的接口或类生成 Mock 对象的类库,它能利用对接口或类的模拟来辅助单元测试。 Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂...
Google_Mock_启蒙篇 当你写一个原型戒是测试的时候,直接去依赖真实的对象通常是丌可行的戒是丌明智的。 Mock 对象实现不真实对象有着相同的接口,但你可以去指定 Mock 对象在运行时它做什么
引入你要用到的GoogleMock名称.除宏或其它特别提到的之外所有GoogleMock名称都位于*testing*命名空间之下.建立模拟对象(Mock Obj
它提供对接口的模拟,能够通过录制、回放、检查三步来完成大体的测试过程,可以验证方法的调用种类、次数、顺序,可以令 Mock 对象返回指定的值或抛出指定异常。通过 EasyMock,我们可以方便的构造 Mock 对象从而使...
EasyMock 是一套通过简单的方法对于指定的接口或类生成 Mock 对象的类库,它能利用对接口或类的模拟来辅助单元测试。本文将对 EasyMock 的功能和原理进行介绍,并通过示例来说明如何使用 EasyMock 进行单元测试。 ...
4.2.4 使用it来访问mock对象本身 4.3 基于行为的jmockit测试 4.3.1 Mock对象 4.3.2 mockit.Expectations 4.3.3 声明和使用mock对象 4.4 Mock对象的约束 4.4.1 调用次数约束 4.4.2 参数约束 4.4.3 返回值的指定 4.5 ...
数据类型、模型或节点——这些都只是mock对象可承担的角色。但mock在单元测试中扮演一个什么角色呢?有时,你需要为单元测试的初始设置准备一些“其他”的代码资源。但这些资源兴许会不可用,不稳定,或者是使用起来...
Spring针对J2EE的常用Web接口提供了Mock,这些组件被发布于spring-mock.jar,介绍如下: MockHttpServletRequest:HttpServletRequest接口...说明:在提供关于Controller(控制器)的测试时,以上这些对象是最常用的。