JUnit Tutorial
Introduction to Junit JUnit Set up JUnit Architecture JUnit Annotations JUnit Fixtures Junit Assertions Junit Categories @Test Annotation Parameters Verification of Exceptions Ignoring tests Time out in JUnit tests Parameterizing tests Test Suite TestWatcher TemporaryFolder ExternalResource Theories in JUnit JUnit Test Runners Execution order of JUnit tests Assumptions in JUnit JUnit and Hamcrest Matchers Running JUnit tests in parallel JUnit and Maven Integration JUnit and Gradle Integration Executing Selenium tests using JUnit test framework Method interceptor and usage JUnit in Intellij IDEA JUnit in Eclipsetemporary folder in junit
Temporary folder rule allows you to create a temporary folder every time you run the test. Below example demonstrates how we can use temporary folder rule. Notice that in both tests, a new temporary folder is created.
package rules;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
public class TempFolderRule {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
System.out.println("Creating a simple file in a temp folder");
File testFile = tempFolder.newFile("testfile.txt");
System.out.println(testFile.getAbsolutePath());
System.out.println("Creating a simple folder in a temp folder");
File testfolder = tempFolder.newFolder("testfolder");
System.out.println("Deleting temp folder..No need to delete explicitly");
tempFolder.delete();
}
@Test
public void testAnotherTempFolder() throws IOException {
System.out.println("Creating a other file in a temp folder");
File testFile = tempFolder.newFile("otherTestfile.txt");
System.out.println(testFile.getAbsolutePath());
}
}
Here is the output of above example.
Creating a simple file in a temp folder
C:UsersSagarAppDataLocalTempjunit1357048258081112491 estfile.txt
Creating a simple folder in a temp folder
Deleting temp folder..No need to delete explicitly
Creating a other file in a temp folder
C:UsersSagarAppDataLocalTempjunit315837697325521595ot
Web development and Automation testing
solutions delivered!!