VOOZH about

URL: https://www.javacodegeeks.com/2015/01/testing-with-files-and-directories-in-junit-with-rule.html

⇱ Testing with files and directories in JUnit with @Rule - Java Code Geeks


Testing with Files and directories in JUnit is easy thanks to TemporaryFolder @Rule.

In JUnit rules (@Rule) can be used as an alternative or an addition to fixture setup and cleanup methods (org.junit.Before, org.junit.After, org.junit.BeforeClass, and org.junit.AfterClass), but they are more powerful, and can be more easily shared between projects and classes.

The code to be tested

public void writeTo(String path, String content) throws IOException {
 Path target = Paths.get(path);
 if (Files.exists(target)) {
 throw new IOException("file already exists");
 }
 Files.copy(new ByteArrayInputStream(content.getBytes("UTF8")), target);
}

The above method can write a given String content to a non existing file. There are two cases that can be tested.

The Test

public class FileWriterTest {

 private FileWriter fileWriter = new FileWriter();

 @Rule
 public TemporaryFolder temporaryFolder = new TemporaryFolder();

 @Rule
 public ExpectedException thrown = ExpectedException.none();

 @Test
 public void throwsErrorWhenTargetFileExists() throws IOException {
 // arrange
 File output = temporaryFolder.newFile("output.txt");

 thrown.expect(IOException.class);
 thrown.expectMessage("file already exists");

 // act
 fileWriter.writeTo(output.getPath(), "test");
 }

 @Test
 public void writesContentToFile() throws IOException {
 // arrange
 File output = temporaryFolder.newFolder("reports")
 .toPath()
 .resolve("output.txt")
 .toFile();

 // act
 fileWriter.writeTo(output.getPath(), "test");

 // assert
 assertThat(output)
 .hasContent("test")
 .hasExtension("txt")
 .hasParent(resolvePath("reports"));
 }

 private String resolvePath(String folder) {
 return temporaryFolder
 .getRoot().toPath()
 .resolve(folder)
 .toString();
 }
}

The TemporaryFolder rule provides two method to manage files and directories: newFile and newFolder. Both method return desired object under the temporary folder created in the setup method. In case the path of the temporary folder itself is needed getRoot method of TemporaryFolder can be used.

Everything that will be added to the temp folder during tests will be automatically removed when test finishes, despite it was successful or not.

This example can be found in my unit-testing-demo project on GitHub along with many other examples.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Rafal Borowiec
Rafal Borowiec
January 14th, 2015Last Updated: January 14th, 2015
1 2,188 1 minute read

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
10 years ago

How can I use your approach to open an existing json file for reading in Junit test?

0
Reply
Back to top button
Close
wpDiscuz