TDD之Dummy Stub Fake Mock
2012-09-18 08:19
459 查看
测试驱动大家都很熟悉了,这两天正好看了一个java的书,对TDD中的一些基本概念进行了复习,具体如下:
@Test
public void tenPercentDiscount() {
String dummyName = "Riley";
Ticket ticket = new Ticket(dummyName, new BigDecimal("10"));
assertEquals(new BigDecimal("9.0"), ticket.getDiscountPrice());
}
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");
}
}
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.
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();
}
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 anobject 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 implementationMost 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();
}
相关文章推荐
- [转]TDD之Dummy Stub Fake Mock
- 测试驱动开发中的Dummy, Stub, Spy, Mock, Fake
- Dummy,Fake,Stub,Mock区别
- Dummy,Fake,Stub和Mock
- TDD学习笔记【六】一Unit Test - Stub, Mock, Fake 简介
- TDD:什么是桩(stub)和模拟(mock)?
- Dummary、Fake、Stub、Spy、Mock介绍
- Dummary、Fake、Stub、Spy、Mock
- 30天快速上手 TDD Day 7 - Unit Test - Stub, Mock, Fake 簡介
- Dummy,Stub,Fake,Mock简介
- iOS測试——置换測试: Mock, Stub 和其它
- TDD- Secton 7 Why Mock Objects?
- stub和mock的实际应用
- What's the difference between a stub and mock?
- mock和fake的区别
- Mock与Stub的区别
- 单元测试之Stub和Mock
- 软件测试中Mock和Stub
- mock和fake的区别
- RhinoMocks : Stub和Mock的区别