Skip to content

Commit 10a863e

Browse files
committed
Improve message if file exists instead of folder
When TemporaryFolder(String path) or TemporaryFolder(String... paths) is called with a path that matches the path of an existing file then an IOException with a message like "a file with path <path> exists" is thrown.
1 parent 01d7ec2 commit 10a863e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/main/java/org/junit/rules/TemporaryFolder.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,13 @@ public File newFolder(String... paths) throws IOException {
206206

207207
lastMkdirsCallSuccessful = file.mkdirs();
208208
if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
209-
throw new IOException(
210-
"could not create a folder with the path \'" + relativePath.getPath() + "\'");
209+
if (file.exists()) {
210+
throw new IOException(
211+
"a file with the path \'" + relativePath.getPath() + "\' exists");
212+
} else {
213+
throw new IOException(
214+
"could not create a folder with the path \'" + relativePath.getPath() + "\'");
215+
}
211216
}
212217
}
213218
if (!lastMkdirsCallSuccessful) {

src/test/java/org/junit/rules/TemporaryFolderUsageTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void newFolderWithGivenFolderThrowsIOExceptionIfFileExists() throws IOExc
9090
assertTrue("Could not create" + file, file.createNewFile());
9191

9292
thrown.expect(IOException.class);
93-
thrown.expectMessage("could not create a folder with the path 'level1'");
93+
thrown.expectMessage("a file with the path 'level1' exists");
9494
tempFolder.newFolder("level1");
9595
}
9696

0 commit comments

Comments
 (0)