Search code examples
javafilesystemsmockingeasymock

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. 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


Solution

  • 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);