[转]TDD之Dummy Stub Fake Mock
TDD之Dummy Stub Fake Mock
测试驱动大家都很熟悉了,这两天正好看了一个java的书,对TDD中的一些基本概念进行了复习,具体如下:
Dummy |
An object that is passed around but never used. Typically used to fulfill the parameter list of a method. |
Stub |
An object that always returns the same canned response. May also hold some dummy state. |
Fake |
An actual working implementation (not of production quality or configuration) that can replace the real implementation. |
Mock |
An object that represents a series of expectations and provides canned responses. |
Dummy
A dummy object is the easiest of the four test double types to use. Remember, it’s designed to help fill parameter lists or fulfill some mandatory field requirements where you know the object will never get used. In many cases, you can even pass in an empty or null object.
@Test
public void tenPercentDiscount() {
String dummyName = "Riley";
Ticket ticket = new Ticket(dummyName, new BigDecimal("10"));
assertEquals(new BigDecimal("9.0"), ticket.getDiscountPrice());
}
Stub object
You typically use a stub object when you want to replace a real implementation with an
object that will return the same response every time. Let’s return to our theater ticket
pricing example to see this in action.
@Test
public void tenPercentDiscount() {
Price price = new StubPrice();
Ticket ticket = new Ticket(price);
assertEquals(9.0,
ticket.getDiscountPrice().doubleValue(),
0.0001);
}
public class StubPrice implements Price {
@Override
public BigDecimal getInitialPrice() {
return new BigDecimal("10");
}
}
Fake object
A fake object can be seen as an enhanced stub that almost does the same work as your production code, but that takes a few shortcuts in order to fulfill your testing require -ments. Fakes are especially useful when you’d like your code to run against something that’s very close to the real third-party subsystem or dependency that you’ll use in the live implementation
Most well-grounded Java developers will sooner or later have to write code that interacts with a database, typically performing CRUD operations on Java objects. Prov-ing that your Data Access Object ( DAO) code works before running it against the pro-duction database is often left until the system integration test phase, or it isn’t checked at all! It would be of great benefit if you could check that the DAO code works during your unit test or integration test phase, giving you that all important, fast feedback.
A fake object could be used in this case—one that represents the database you’re interacting with. But writing your own fake object representing a database would be quite difficult! Luckily, over recent years, in-memory databases have become small enough, lightweight enough, and feature-rich enough to act as a fake object for you.
HSQLDB (www.hsqldb.org) is a popular in-memory database used for this purpose.
Mock object
Mock objects are related to the stub test doubles that you’ve already met, but stub objects are usually pretty dumb beasts. For example, stubs typically fake out methods so that they always give the same result when you call them. This doesn’t provide any way to model state-dependent behavior.
Mockito (available from http://mockito.org/
@Test
public void tenPercentDiscount() {
Price price = mock(Price.class);
when(price.getInitialPrice()).
➥ thenReturn(new BigDecimal("10"));
Ticket ticket = new Ticket(price, new BigDecimal("0.9"));
assertEquals(9.0, ticket.getDiscountPrice().doubleValue(), 0.000001);
verify(price).getInitialPrice();
}
退出登录 订阅评论
[Ctrl+Enter快捷键提交]
[转]TDD之Dummy Stub Fake Mock的更多相关文章
- Stub和Mock的理解
我对Stub和Mock的理解 介绍 使用测试驱动开发大半年了,我还是对Stub和Mock的认识比较模糊,没有进行系统整理. 今天查阅了相关资料,觉得写得很不错,所以我试图在博文中对资料进行整理一下,再 ...
- 我对Stub和Mock的理解
介绍 使用测试驱动开发大半年了,我还是对Stub和Mock的认识比较模糊,没有进行系统整理. 今天查阅了相关资料,觉得写得很不错,所以我试图在博文中对资料进行整理一下,再加上一些自己的观点. 本文是目 ...
- What's the difference between a stub and mock?
I believe the biggest distinction is that a stub you have already written with predetermined behavio ...
- 使用 Moq 测试.NET Core 应用 - Why Moq?
什么是Mock 当对代码进行测试的时候, 我们经常需要用到一些模拟(mock)技术. 绿色的是需要被测试的类, 黄色是它的依赖项, 灰色的无关的类 在一个项目里, 我们经常需要把某一部分程序独立出来以 ...
- Go项目的测试代码3(测试替身Test Double)
上一篇文章介绍了项目中测试代码的写法. Go项目的测试代码2(项目运用) 这里简单的共享一下测试替身. 当我们写测试代码的时候,经常遇到一个问题.跟别的模块或服务有依赖性,可是功能还没开发完.或是因为 ...
- TDD学习笔记【六】一Unit Test - Stub, Mock, Fake 简介
这篇文章简介一下,如何通过 mock framework,来辅助我们更便利地模拟目标对象的依赖对象,而不必手工敲堆只为了这次测试而存在的辅助类型. 而模拟目标对象的部分,常见的有 stub objec ...
- TDD:什么是桩(stub)和模拟(mock)?
背景 本文假设你对TDD.Stub和Mock已经有了初步的认识,本文简单解释一下stub和mock的区别和使用场景,先看看他们之间的关系: 由上图可以知道mock框架可以非常容易的开发stub和moc ...
- [转]软件测试- 3 - Mock 和Stub的区别
由于一直没有完全搞明白Mock和Stub的区别,所以查了很多文章,而这一篇是做好的: http://yuan.iteye.com/blog/470418 尤其是8楼,Frostred的发言,描述地相当 ...
- nodejs unit test related----faker-cli, sinonjs, mock/stub
http://www.tuicool.com/articles/rAnaYvn http://www.tuicool.com/articles/Y73aYn (contrast stub and mo ...
随机推荐
- jqgrid取所有行的值,jqgrid取行对应列(name)的值,jqgrid取多行值对应列转json的方法
1.jqgrid取所有行的值(#gridTable指对应table的ID) var obj = $("#gridTable").jqGrid("getRowData&qu ...
- 错误:this is incompatible with sql_mode=only_full_group_by
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'H5APP_WORK ...
- 【转】MVC form提交实体接收参数时空字符串值变成null
问题:entity.BZ的值是null,Request.Form["BZ"]的值是空字符串 目标:让entity.BZ的值是空字符串. 解决方法:在实体的BZ属性上加上 [Disp ...
- windows的网上邻居
要说以前中学最怕上什么课就是电脑课,要说电脑印象最深的是什么软件就是总摆在桌面又听不懂又点进去什么都没有的“网上邻居”. “网上邻居”指的是同一局域网内在线的的电脑,更简单些就是连在同一路由器中开机的 ...
- 码云git使用四(分支的创建,使用和合并)
我们在使用git的时候,一般都不是直接在主代码中开发, 通常我们做的操作是创建一个分支,我们在分支上开发,开发完毕,在提交到主代码中. 我们现在学习创建分支,合并分支. 1.首先我们下载到本地都是在主 ...
- 判断window.open的页面是否已经被关
<!DOCTYPE html><html><head><meta charset="utf-8"><title>菜鸟教程 ...
- 关于JAVA的一些知识点
1.java.lang.Runtime.getRuntime().availableProcessors() Returns the number of processors available to ...
- py propterties reuqest.post
import tracebackclass Properties(object): def __init__(self, fileName): self.fileName = fileName sel ...
- 尚学堂java答案解析 第一章
本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改 一.选择题: 1.C 解析:java为了安全,中并没有引入C语言的指针概念. 2.AD 解析:B:Java先通过ja ...
- 自定义xadmin后台首页
登陆xadmin后台,首页默认是空白,可以自己添加小组件,xadmin一切都是那么美好,但是添加小组件遇到了个大坑,快整了2个礼拜,最终实现想要的界面.初始的页面如图: 本机后台显示这个页面正常,do ...