如何使用 EasyMock 模拟文件?
我最近接触到了 EasyMock,并被要求使用它为 FileMonitor 类开发一些单元测试。 FileMonitor 类基于定时事件,该事件唤醒并检查已定义的文件和目录列表中的文件修改。我了解如何使用实际的文件系统来执行此操作,编写一个写入文件的测试并让 FileMonitor 执行其操作。那么,我该如何使用 EasyMock 来做到这一点呢?我只是不明白如何让 EasyMock 模拟文件系统。
谢谢, 托德
I have recently been introduced to EasyMock and have been asked to develop some unit tests for a FileMonitor class using it. The FileMonitor class is based on a timed event that wakes up and checks for file modification(s) in a defined list of files and directories. I get how to do this using the actual file system, write a test that writes to a file and let the FileMonitor do its thing. So, how do I do this using EasyMock? I just don't get how to have EasyMock mock the file system.
Thanks,
Todd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大致如下:
Something along the lines of:
查看优秀(且简洁)的用户指南。不过,您可能会重新考虑使用 EasyMock - 大多数人当前正在使用或正在切换到更高级、更积极开发的 Mockito(受 EasyMock 启发)。
Have a look at the excellent (and concise) user guide. You might reconsider using EasyMock though - most people are currently using or in the process of switching to the more advanced and more actively developed Mockito (inspired by EasyMock).
模拟的基本技术是引入一个接口(如果当前设计没有),该接口为正在模拟的真实服务(依赖项)提供方法。该测试正在测试被测类是否与依赖项正确交互。这里的正确意味着它执行了您期望它执行的操作。这并不意味着它做了正确的事情,因为正确的事情只能通过使用真实组件的集成测试来确定(您设想通过创建真实文件来做什么)。
因此,您需要在被测类上有一个方法,让您可以传递该接口的实现。最明显的是通过构造函数。您拥有生产构造函数,它使用命中真实文件系统的接口的实际实现来初始化类,然后在测试下将模拟传递给构造函数。
在测试中,您在类上运行方法并断言接口是按照您期望的方式调用的。
我要指出的是,在创建类并通过模拟进行单元测试之后进行的价值有限,但它将有助于锁定行为,以便将来对类的更改不会以令人惊讶的方式打破预期。
我希望这有助于您入门。
一些模拟框架支持模拟实际的具体类,这在单元测试后的测试中非常有意义(通过拦截对真实类的调用而不仅仅是接口)。我找不到 EasyMock 是否允许您这样做,但是如果您需要,JDave 可能是您的最佳选择那种功能。它甚至可以让你模拟期末课程。
The basic technique for mocking is to introduce an interface (if the current design doesn't have one) that provides methods for the real service (the dependency) that is being mocked. The test is testing that the class under test interacts correctly with the dependency. Correctly here means that it does what you expect it to do. That does not mean it does the right thing, as the right thing can only be determined by an integration test that uses the real components (what you envision doing by creating a real file).
So you need to have a method on the class under test that lets you pass in an implementation of this interface. The most obvious is via the constructor. You have the production constructor which initializes the class with the real implementation of the interface that hits the real file system, and then under test you pass in the mock to the constructor.
In the test you run the methods on the class and assert that the interface was called in the way you expect.
I will note that coming along after a class is creating and unit testing via mocks is of limited value, but it will help lock down behavior so that future changes to the class won't break expectations in surprising ways.
I hope that helps get you started.
Some mocking frameworks support mocking actual concrete classes, which can make a lot of sense in test-after unit tests (by intercepting calls to real classes not just interfaces). I couldn't find if EasyMock lets you do that, but JDave is probably the place to go if you need that kind of functionality. It even lets you mock final classes.
我将对文件系统的实际调用放在其单独的包私有方法中。为了进行测试,请扩展该类并重写该方法。因此,您实际上并没有调用文件系统。
EasyMocks 类扩展还可以创建部分模拟,但我并不完全相信这一点。
http://easymock.org/EasyMock2_4_ClassExtension_Documentation.html
I would put the actual call to the filesystem in its separate package-private method. For testing, extend the class and override that method. Thus you do not actually make a call to the file system.
EasyMocks classextension has also the possibility to create paritial mocks, but I'm not totally convinced of that.
http://easymock.org/EasyMock2_4_ClassExtension_Documentation.html