Simulate a file in Java

I am trying to write unit tests for a method that takes a String file name, then opens a file and reads it. So, to test this method, I thought about writing a file and then called my method. However, in an assembly farm, it is not possible to write files arbitrarily to disk. Is there a standard way to "simulate" a real file in my unit test?

+8
java unit-testing
source share
4 answers

This is very disapproving:

The smallest amount of code checked. Often one method / function, without using other methods or classes. Fast! Thousands of units of tests can be performed in ten seconds or less! A unit test NEVER uses:

  • database
  • application server (or server of any type)
  • file / network input / output or file system;
  • another application;
  • console (System.out, System.err, etc.)
  • entry
  • most other classes (exceptions include DTO, String, Integer, mocks, and possibly a few others). "

A source

If you must read from a file, try creating a test file from which all unit tests will be read. No need to write anything to disk.

-one
source share

I found that Mockito and Powermock are a good combination for this. In fact, there is a blog post with a sample where the constructor of the File class mocks the testing goals. Here is a small example that I put together:

public class ClassToTest { public void openFile(String fileName) { File f = new File(fileName); if(!f.exists()) { throw new RuntimeException("File not found!"); } } } 

Testing with Mockito + Powermock:

 @RunWith(PowerMockRunner.class) @PrepareForTest(ClassToTest.class) public class FileTest { @Test public void testFile() throws Exception { //Set up a mocked File-object File mockedFile = Mockito.mock(File.class); Mockito.when(mockedFile.exists()).thenReturn(true); //Trap constructor calls to return the mocked File-object PowerMockito.whenNew(File.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(mockedFile); //Do the test ClassToTest classToTest = new ClassToTest(); classToTest.openFile("testfile.txt"); //Verify that the File was created and the exists-method of the mock was called PowerMockito.verifyNew(File.class).withArguments("testfile.txt"); Mockito.verify(mockedFile).exists(); } } 
+16
source share

How about using cool stream classes that completely override real ones (like BufferredReader, File) (which means all methods or all the methods you use)? Data can be stored as an array of bytes, for example, in some singleton, if they should be used between different test classes.

0
source share

If you are using JUnit, there is a TemporaryFolder . Files are deleted after the test. Example shown on page:

 public static class HasTempFolder { @Rule public TemporaryFolder folder= new TemporaryFolder(); @Test public void testUsingTempFolder() throws IOException { File createdFile= folder.newFile("myfile.txt"); File createdFolder= folder.newFolder("subfolder"); // ... } } 

However, I also used it to test the read and write capabilities of the Android class, for example:

  [...] pw = new PrintWriter(folder.getAbsolutePath() + '/' + filename); pw.println(data); 
0
source share

All Articles