简体   繁体   English

如何使用 EasyMock 模拟文件?

[英]How to mock a file with EasyMock?

I have recently been introduced to EasyMock and have been asked to develop some unit tests for a FileMonitor class using it.我最近被介绍到 EasyMock,并被要求使用它为 FileMonitor 类开发一些单元测试。 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. FileMonitor 类基于定时事件,该事件唤醒并检查定义的文件和目录列表中的文件修改。 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.我知道如何使用实际的文件系统来做到这一点,编写一个写入文件的测试并让 FileMonitor 做它的事情。 So, how do I do this using EasyMock?那么,我如何使用 EasyMock 做到这一点? I just don't get how to have EasyMock mock the file system.我只是不知道如何让 EasyMock 模拟文件系统。

Thanks, Todd谢谢,托德

Something along the lines of:类似的东西:

import static org.easymock.classextension.EasyMock.*;

File testDir = createMock(File.class);
expect(testDir.lastModified()).andReturn(10L);
// more expectations
replay(testDir);
// create a FileMonitor watching testDir
// run the method which gets invoked by the trigger     
verify(testDir);

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 - 大多数人目前正在使用或正在切换到更先进和更积极开发的Mockito (受 EasyMock 启发)。

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.我不知道 EasyMock 是否可以让您这样做,但如果您需要这种功能, JDave可能是您要去的地方。 It even lets you mock final classes.它甚至可以让您模拟最终课程。

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. EasyMocks classextension 也可以创建局部模拟,但我并不完全相信这一点。

http://easymock.org/EasyMock2_4_ClassExtension_Documentation.html http://easymock.org/EasyMock2_4_ClassExtension_Documentation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM