Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 416
Todd Avatar asked Feb 27 '23 04:02

Todd


2 Answers

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);
like image 95
Binil Thomas Avatar answered Mar 08 '23 08:03

Binil Thomas


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

like image 45
Bozhidar Batsov Avatar answered Mar 08 '23 09:03

Bozhidar Batsov