TDD之Dummy Stub Fake Mock

简介: 测试驱动大家都很熟悉了,这两天正好看了一个java的书,对TDD中的一些基本概念进行了复习,具体如下: Dummy An object that is passed around but never used.

测试驱动大家都很熟悉了,这两天正好看了一个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();

  }

相关文章
|
Kubernetes 测试技术 容器
实践 Fake ClientSet 单元测试
在 Kubernetes 相关的开发中,client-go 是最常用的,对于 client-go 相关的代码我们可以通过 fake ClientSet 来编写单元测试,本文将实践利用 fake ClientSet
1217 0
|
4月前
|
JavaScript 数据安全/隐私保护
|
5月前
|
Cloud Native 测试技术 Go
什么是单元测试(unit testing)
什么是单元测试(unit testing)
|
测试技术 PHP
TP5.0安装testing 单元测试 报错
原因:使用了比较高版本的php,topthink/tesing v1.x仅限php7.1使用 太高太低都会出现报错
92 0
|
敏捷开发 设计模式 Java
mock打桩之EasyMock
TDD是测试驱动开发(Test-Driven Development)的英文简称,是敏捷开发中的一项核心实践和技术,也是一种设计方法论。TDD的原理是在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码。TDD虽是敏捷方法的核心实践,但不只使用于XP(Extreme Programming),同样可以适用于其他开发方法和过程。
mock打桩之EasyMock
|
测试技术
SAP Spartacus Delivery Mode Component单元测试的Mock设计图
SAP Spartacus Delivery Mode Component单元测试的Mock设计图
88 0
SAP Spartacus Delivery Mode Component单元测试的Mock设计图
|
存储 供应链 编译器
玩转 Objective-C 的 Mock 对象
测试驱动开发(TDD)中,开发者经常使用模拟对象进行系统设计,模拟对象到底是什么呢?部分模拟对象和全部模拟对象又是什么呢?模拟对象真的让人又爱又恨吗?让我们以Objective-C测试框架OCMock来探个究竟。
179 0
|
Java
Mock使用
SpringBoot/SpringMvc使用Mock/MockBean 做单元测试
2196 0
|
前端开发 测试技术 开发工具
React 16 Jest单元测试 之 Mock Functions(Mock Return Values)
转载地址 React16 Jest单元测试 之 Mock Functions(Mock Return Values) 项目初始化【这里使用之前的项目,节省时间】 项目初始化地址 https://github.
1342 0