• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Mock Objects:缺点和用例

发布: 2009-4-13 09:42 | 作者: 不详 | 来源: 测试时代采编 | 查看: 51次 | 进入软件测试论坛讨论

领测软件测试网

@Test public void shouldAddNewEmployee() {
mockEmployeeDAO.insert(employee);
replay(mockEmployeeDAO);
employeeBO.addNewEmployee(employee);
verify(mockEmployeeDAO);
}
@Test public void shouldUpdateEmployee() {
mockEmployeeDAO.update(employee);
replay(mockEmployeeDAO);
employeeBO.updateEmployee(employee);
verify(mockEmployeeDAO);
}

        测试方法shouldAddNewEmployee用于验证测试对象(employeeBO )和Mock(mockEmployeeDAO )之间交互是否正确。它期望employeeBO调用mockEmployeeDAO中的insert方法,并将接收到的Employee实例传递给该方法。shouldAddNewEmployee方法虽然简单,但其中含有一些没有实际目的的代码,这为我们的测试带来了混乱:

重复调用replay,通知EasyMock所有期望的工作已完成。 重复调用verify,通知EasyMock所有期望的工作已验证完成。

Mock的使用常常遵循下面的模式:

建立Mock和期望 执行要测试的代码 验证执行结果是否符合期望

        这种模式会在测试代码中引入重复,这在测试方法shouldAddNewEmployee()和shouldUpdateEmployee()中很明显。使用 EasyMockTemplate 可以减少代码混乱和重复:

/**
* Understands a template for usage of EasyMock mocks.
* @author Alex Ruiz
*/
public abstract class EasyMockTemplate {
/** Mock objects managed by this template */
private final List<Object> mocks = new ArrayList<Object>();

/**
* Constructor.
* @param mocks the mock objects this template will manage.
* @throws IllegalArgumentException if the list of mock objects is null or empty.
* @throws IllegalArgumentException if the list of mock objects contains a null value.
*/
public EasyMockTemplate(Object... mocks) {
if (mocks == null) throw new IllegalArgumentException("The list of mock objects should not be null");
if (mocks.length == 0) throw new IllegalArgumentException("The list of mock objects should not be empty");
for (Object mock : mocks) {
if (mock == null) throw new IllegalArgumentException("The list of mocks should not include null values");
this.mocks.add(mock);
}
}

/**
* Encapsulates the common pattern followed when using EasyMock.
* <ol>
* <li>Set up expectations on the mock objects</li>
* <li>Set the state of the mock controls to "replay"</li>
* <li>Execute the code to test</li>
* <li>Verify that the expectations were met</li>
* </ol>
* Steps 2 and 4 are considered invariant behavior while steps 1 and 3 should be implemented by subclasses of this template.
*/
public final void run() {
setUp();
expectations();
for (Object mock : mocks) replay(mock);
codeToTest();
for (Object mock : mocks) verify(mock);
}

/** Sets the expectations on the mock objects. */
protected abstract void expectations();

/** Executes the code that is under test. */
protected abstract void codeToTest();

/** Sets up the test fixture if necessary. */
protected void setUp() {}
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

22/2<12

关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网