diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java new file mode 100644 index 0000000000..1380846e24 --- /dev/null +++ b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java @@ -0,0 +1,29 @@ +import com.google.common.io.Files; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +public class CreateFilesTest { + @Test(expected = IOException.class) + public void whenCreatingAFileWithAbsolutePath_thenExceptionIsThrown() throws IOException { + File fileWithAbsolutePath = new File("/myDirectory/testFile.txt"); + + Files.touch(fileWithAbsolutePath); + } + + @Test(expected = IOException.class) + public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFile_thenExceptionIsThrown() throws IOException { + File tempDirectory = new File(System.getProperty("java.io.tmpdir")); + File fileWithRelativePath = new File(tempDirectory, "myDirectory/newFile.txt"); + + Files.touch(fileWithRelativePath); + } + + @Test(expected = IOException.class) + public void whenCreatingAFileWithFileSeparator_thenPathIsCreated() throws IOException { + File newFile = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "newFile.txt"); + + Files.touch(newFile); + } +}