diff --git a/src/main/java/org/junit/rules/TemporaryFolder.java b/src/main/java/org/junit/rules/TemporaryFolder.java index fe0ac38b0f90..1a6a77060805 100644 --- a/src/main/java/org/junit/rules/TemporaryFolder.java +++ b/src/main/java/org/junit/rules/TemporaryFolder.java @@ -206,8 +206,13 @@ public File newFolder(String... paths) throws IOException { lastMkdirsCallSuccessful = file.mkdirs(); if (!lastMkdirsCallSuccessful && !file.isDirectory()) { - throw new IOException( - "could not create a folder with the path \'" + relativePath.getPath() + "\'"); + if (file.exists()) { + throw new IOException( + "a file with the path \'" + relativePath.getPath() + "\' exists"); + } else { + throw new IOException( + "could not create a folder with the path \'" + relativePath.getPath() + "\'"); + } } } if (!lastMkdirsCallSuccessful) { diff --git a/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java b/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java index 925408fa0818..8213e39ede49 100644 --- a/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java +++ b/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java @@ -89,6 +89,17 @@ public void newFolderWithGivenFolderThrowsIOExceptionIfFileExists() throws IOExc File file = new File(tempFolder.getRoot(), "level1"); assertTrue("Could not create" + file, file.createNewFile()); + thrown.expect(IOException.class); + thrown.expectMessage("a file with the path 'level1' exists"); + tempFolder.newFolder("level1"); + } + + @Test + public void newFolderWithGivenFolderThrowsIOExceptionWhenFolderCannotBeCreated() throws IOException { + tempFolder.create(); + assertTrue("Could not make folder " + tempFolder.getRoot() + " read only.", + tempFolder.getRoot().setReadOnly()); + thrown.expect(IOException.class); thrown.expectMessage("could not create a folder with the path 'level1'"); tempFolder.newFolder("level1");