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();

}

好文要顶 关注我 收藏该文
0
0
 
posted @ 2012-09-18 08:19 2012 阅读(1070) 评论(0) 编辑 收藏

发表评论

昵称:

评论内容:
 
昵称:2012
园龄:6年10个月
粉丝:351
关注:15

+加关注
< 2012年9月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6

搜索

 

[转]TDD之Dummy Stub Fake Mock的更多相关文章

  1. Stub和Mock的理解

    我对Stub和Mock的理解 介绍 使用测试驱动开发大半年了,我还是对Stub和Mock的认识比较模糊,没有进行系统整理. 今天查阅了相关资料,觉得写得很不错,所以我试图在博文中对资料进行整理一下,再 ...

  2. 我对Stub和Mock的理解

    介绍 使用测试驱动开发大半年了,我还是对Stub和Mock的认识比较模糊,没有进行系统整理. 今天查阅了相关资料,觉得写得很不错,所以我试图在博文中对资料进行整理一下,再加上一些自己的观点. 本文是目 ...

  3. 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 ...

  4. 使用 Moq 测试.NET Core 应用 - Why Moq?

    什么是Mock 当对代码进行测试的时候, 我们经常需要用到一些模拟(mock)技术. 绿色的是需要被测试的类, 黄色是它的依赖项, 灰色的无关的类 在一个项目里, 我们经常需要把某一部分程序独立出来以 ...

  5. Go项目的测试代码3(测试替身Test Double)

    上一篇文章介绍了项目中测试代码的写法. Go项目的测试代码2(项目运用) 这里简单的共享一下测试替身. 当我们写测试代码的时候,经常遇到一个问题.跟别的模块或服务有依赖性,可是功能还没开发完.或是因为 ...

  6. TDD学习笔记【六】一Unit Test - Stub, Mock, Fake 简介

    这篇文章简介一下,如何通过 mock framework,来辅助我们更便利地模拟目标对象的依赖对象,而不必手工敲堆只为了这次测试而存在的辅助类型. 而模拟目标对象的部分,常见的有 stub objec ...

  7. TDD:什么是桩(stub)和模拟(mock)?

    背景 本文假设你对TDD.Stub和Mock已经有了初步的认识,本文简单解释一下stub和mock的区别和使用场景,先看看他们之间的关系: 由上图可以知道mock框架可以非常容易的开发stub和moc ...

  8. [转]软件测试- 3 - Mock 和Stub的区别

    由于一直没有完全搞明白Mock和Stub的区别,所以查了很多文章,而这一篇是做好的: http://yuan.iteye.com/blog/470418 尤其是8楼,Frostred的发言,描述地相当 ...

  9. 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 ...

随机推荐

  1. Grafana安装配置介绍

    一.Grafana介绍 Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Pr ...

  2. Hadoop介绍-1.基本原理

    了解大数据 首先,搞清楚hadoop在处理大数据的定位在哪里 什么是大数据?为什么要处理大数据? 数据量大(Volume) 数据类别复杂(Variety) 数据处理速度快(Velocity) 数据真实 ...

  3. [洛谷 P2508] 圆上的整点

    题目描述 求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. 输入输出格式 输入格式: r 输出格式: 整点个数 输入输出样例 输入样例#1: 4 输出样例#1: 4 说明 n ...

  4. JQuery操作input

    // 不可编辑 $("#id").attr("disabled","disabled"); $("#id").remov ...

  5. css3 居中(推荐弹性盒模型方式)

    参考  http://www.zhihu.com/question/20774514 http://caibaojian.com/demo/flexbox/align-items.html 例子:ht ...

  6. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  7. 实现django admin后台到xadmin后台的转变

    虽然不做前端,还是喜欢好看的东西~.~ 之前同事估计也是功能实现没空管这个后台,前段时间闲的,稍微改了下外貌,前后对比下: Python3.5+Django1.9.7+Xadmin0.6.1 步骤如下 ...

  8. python使用变量

    #不建议用加号,建议用.format name = input('name:') age = input('age:') print( name ,age) print('姓名:',name,'年龄: ...

  9. 6.4 C++提取子字符串及字符串的比较

    参考:http://www.weixueyuan.net/view/6393.html 总结: 函数substr可以提取string字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串 ...

  10. js正则学习

    一直对正则很纠结也很畏惧,以前感觉花时间理解一个个奇奇怪怪的符号,还不如直接百度谷歌之. 但知其然不知其所以然也是种痛苦,所以花了两天稍微学了一下,虽然没学很深入彻底,但也比之前进步不少,特此笔记. ...