如何用EasyMock模拟文件?

Tod*_*odd 4 java filesystems easymock mocking

我最近被介绍给EasyMock,并被要求使用它为FileMonitor类开发一些单元测试.FileMonitor类基于定时事件,该事件唤醒并检查已定义的文件和目录列表中的文件修改.我得到了如何使用实际的文件系统执行此操作,编写一个写入文件的测试并让FileMonitor执行其操作.那么,我如何使用EasyMock做到这一点?我只是不知道如何让EasyMock模拟文件系统.

谢谢,托德

Bin*_*mas 6

有点像:

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);
Run Code Online (Sandbox Code Playgroud)