Wednesday, 4 July 2018

Junit: TemporaryFolder rule

By using TemporaryFolder rule, you can create temporary files and folders, those can be deleted once the test cases is executed (The execution status pass/fail doesn't matter).

TempFolderTest.java
package com.sample.test;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class TempFolderTest {

 @Rule
 public TemporaryFolder tempFolder= new TemporaryFolder();
 
 @Test
 public void testTempFolder() throws IOException {
  File folder = tempFolder.newFolder("demos");
  File file = tempFolder.newFile("Hello.txt");
  
  assertEquals(folder.getName(), "demos");
  assertEquals(file.getName(), "Hello.txt");
  
 }
 
 
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment