Mock和Stub(JMock)有什么区别 [英] What is the difference between mocks and stubs ( JMock)

查看:324
本文介绍了Mock和Stub(JMock)有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jMock中的模拟和存根之间有什么区别?我可以用jMock都创建吗?我如何用它创建存根,以及最适合这种情况的是什么,我相信使用存根是我需要准备一些状态进行测试的时候.

What is the difference between mocks and stubs in jMock? I can create both with jMock? how i can create stubs with it and what the situation is most appropriate for this, I believe that using stubs is when I need to prepare some state for test.

谢谢

推荐答案

Wikipedia的文章关于模拟对象,但术语解释得不够好.我们曾经进行过这种区分(当然,可能需要讨论):

Wikipedia has an article regarding Mock objects, but the terminology is not explained as good as could be. We used to make this distinction (which may be subject to discussion, of course):

模拟和存根都模拟了测试组件所需的对象.

Mocks and stubs both simulate an object which is required for testing a component.

当您想断言被测组件与模拟对象之间发生特定类型的交互时,会使用模拟"一词.这就是为什么模拟框架(如EasyMock)提供方法来断言实际上所有预期的调用都已执行.例如您想看到您的服务实际上调用了一个(模拟的)DAO.因此,此调用是您的测试条件/断言的一部分.

The word "mock" is used when you want to assert that a specific kind of interaction between the tested component and the mocked object takes place. That's why mock frameworks (like EasyMock) provide methods to assert that all expected calls have actually been performed. E. g. you want to see that your service actually calls a (mocked) DAO. So this call is part of your test conditions / assertions.

但是,当您只是试图提供有助于测试组件的实现时,会使用存根"一词.发生什么类型的交互都没有关系,您只需要存根填补空白即可测试组件.您的重点在于经过测试的组件及其功能.

The word "stub" however is used when you are simply trying to provide an implementation which helps testing your component. What kind of interaction takes place does not matter, you just want the stub to fill in the gaps so you can test your component. Your focus lies on the tested components and what it does.

对于同一件事,这只是两个字,具体取决于您要实现的目标.

So it's just two words for the same thing, depending on what you are trying to achieve with it.

这篇关于Mock和Stub(JMock)有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆