单体测试mock对象工具简介

本文介绍了Mock对象的概念及其在单元测试中的应用,并对比了几种常用的Mock对象生成工具,包括EasyMock、MockMaker、MockDoclet、jMock及MockCreator等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)什么是mock对象?

from: http://www.easymock.org/

Unit testing is the testing of software units in isolation. However, most units do not work alone, but they collaborate with other units. To test a unit in isolation, we have to simulate the collaborators in the test.

A Mock Object is a test-oriented replacement for a collaborator. It is configured to simulate the object that it replaces in a simple way. In contrast to a stub, a Mock Object also verifies whether it is used as expected. Tim Mackinnon, Steve Freeman and Philip Craig pioneered the concept of Mock Objects, and coined the term. They presented it at the XP2000 conference in their paper Endo Testing: Unit Testing with Mock Objects. A reworked version of the paper is published in the book Extreme Programming Examined (Addison-Wesley).

(2)EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for them. It was presented for the first time at OOPSLA 2001.

EasyMock为Mock Objects提供接口并在JUnit测试中利用Java的proxy设计模式生成它们的实例。EasyMock最适合于测试驱动开发。 (from: http://www.open-open.com/open64643.htm

(3)about mockmaker,from http://mockmaker.sourceforge.net/, we see that,

MockMaker is a program for creating source code for mock object classes. Given an interface, it writes the source code for a mock object class that implements the interface and allows instances of that class to have expectations set about how many times a method is called, what parameters each method is called with, and to pre-set return values for methods. In many cases (possibly most cases), the classes produced by MockMaker are exactly what you want a mock class to do.

(4)about mockDoclet,MockDoclet is an active code generation tool for automatically generating mock objects implementation classes from existing classes or interfaces in Java.(from:http://joe.truemesh.com/mockdoclet/

(5)about jmock,jMock is a library for testing Java code using mock objects1.(from:http://www.jmock.org/

(6)What is the MockCreator? MockCreator, first coded at German company Abstrakt Gmbh, and enhanced by me, Vladimir Dyuzhev, is the generator of mocks. Given an interface or a class, it generates source of mock class for it. however, this project has been cancelled due to lack of time, what a pity.(see it at http://mockcreator.sourceforge.net/).

(7)About Mock Objects, a technique for improving the design of code within Test-Driven Development, see it at http://www.mockobjects.com/

(8)mock入门可以参考:Mock Object工具横向比较(http://java.ccidnet.com/art/3737/20051017/472031_1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值