java - How do I unit-test saving file to the disk with FileUtils? -


i know variation has been asked before.

but, 1 of functions using common's fileutils. here, takes file object , string. there way unit test this?

of course. use temporary folder, save file there, , delete folder after test.

if use junit, @ temporaryfolder junit rule (it creates temp folder , takes care of deleting).

example code:

public class yourtest {   @rule   public temporaryfolder folder= new temporaryfolder();    @test   public void testusingtempfolder() throws ioexception {       string filepath = folder.newfile("myfile.txt").getabsolutepath();       fileutils.writefile(filepath, "some string");       asserttrue(new file(filepath).exists());   } } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -