lemoncodes
lemoncodes

Reputation: 2421

Mocking file reading/writing via JUnit

How do you mock file reading/writing via JUnit?

Here is my scenario

MyHandler.java

public abstract class MyHandler {

    private String path = //..path/to/file/here

    public synchronized void writeToFile(String infoText) {
        // Some processing
        // Writing to File Here
        File file = FileUtils.getFile(filepath);
        file.createNewFile();
        // file can't be written, throw FileWriteException
        if (file.canWrite()) {
            FileUtils.writeByteArrayToFile(file, infoText.getBytes(Charsets.UTF_8));
        } else {
            throw new FileWriteException();
        }
    }

    public String readFromFile() {
        // Reading from File here
        String infoText = "";
        File file = new File(path);
        // file can't be read, throw FileReadException
        if (file.canRead()) {
            infoText = FileUtils.readFileToString(file, Charsets.UTF_8);        
        } else {
            throw FileReadException();
        }

        return infoText
    }

}

MyHandlerTest.java

@RunWith(PowerMockRunner.class)
@PrepareForTest({
    MyHandler.class
})
public class MyHandlerTest {

    private static MyHandler handler = null;
    // Some Initialization for JUnit (i.e @Before, @BeforeClass, @After, etc)

    @Test(expected = FileWriteException.class)
    public void writeFileTest() throws Exception {

       handler.writeToFile("Test Write!");

    }

    @Test(expected = FileReadException.class)
    public void readFileTest() throws Exception {

       handler.readFromFile();

    }
}

Given above source, Scenario when file is not writable (write permission not allowed) is OK, However, when i try to do scenario wherein file is not readable (read permission not allowed). It always read the file, i have already tried to modify the file permission on the test code via below

File f = new File("..path/to/file/here");
f.setReadable(false);

However, I did some reading, setReadable() always returns false (failed) when run on Windows machine.

Is there a way to modify the file permission of the target file programmatically in relation to JUnit?

Note

Target source code to test cannot be modified, meaning Myhandler.class is a legacy code which is not to be modified.

Upvotes: 3

Views: 4897

Answers (3)

Alex
Alex

Reputation: 7926

In jUnit there's a handy rule for scenarios like yours.

public class MyHandlerTest {

    @Rule
    // creates a temp folder that will be removed after each test
    public org.junit.rules.TemporaryFolder folder = new org.junit.rules.TemporaryFolder();

    private MyHandler handler;

    @Before
    public void setUp() throws Exception {
        File file = folder.newFile("myFile.txt");
        // do whatever you need with it - fill with test content and so on.
        handler = new MyHandler(file.getAbsolutePath()); // use the real thing
    }

    // Test whatever behaviour you need with a real file and predefined dataset.
}

Upvotes: 0

m0skit0
m0skit0

Reputation: 25873

Since Mockito cannot mock static methods, use a File factory instead (or refactor your FileUtils to be a factory), then you can mock it and return a mocked File instance as well, where you can also mock any File methods you want.

So instead of FileUtils.getFile(filepath) you will now have something like FileFactory.getInstance().getFile(filepath) for example, where you can mock getFile(String) method easily.

Upvotes: 0

Torben
Torben

Reputation: 3913

Instead of relying on the operating system file permissions, use PowerMock to mock FileUtils.getFile(...) and make it return an instance of File (e.g. anonymous sub class) that returns a specific value for canWrite()/canRead().

Mocking static methods with Mockito

Upvotes: 2

Related Questions