How do I test File api in Junit

Reading Time: < 1 minute

I was looking for a way to test Java file apis, especially I’ve such methods which return Java File. I somehow felt that those methods also have to be tested at least a small test case could be conducted. I did check what File api returns at Oracle Api Docs. The Method you’ll observe below called “buildExcelForRemove” method takes up an ArrayList as a parameter and does process the list and produces a File object as an outcome.

So finally I’ve ended up conducting below test case which you will find very simple. I controversially test;

  • the array list to be given to the corresponding method whether it is empty,
  • size of the array list,
  • existence of the file,
  • whether the outcome is a file,
  • given filename in the method does match the produced file’s name,
  • whether the file object is empty.

After I fire up the test case, I observe that the conducted test is successful.

	private File file;	

	@Before
	public void setUp() throws Exception {
	
	}

	@After
	public void tearDown() throws Exception {
		
	}

	@Test
	public void stage01_BuildExcelForRemoveWithData() {
		file = officeService.buildExcelForRemove(fullreportDetails);
		assertNotNull(fullreportDetails);
		assertEquals(1, fullreportDetails.size());
		assertTrue(file.exists());
		assertTrue(file.isFile());
		assertEquals(buildExcelForRemoveFileName, file.getName());
		assertNotNull(file);
	}