MightySpec
MightySpec

Reputation: 31

Temporary folder using JUnit's Rule annotation

I'm using JUnit's TemporaryFolder class rule to create a temporary folder during JUnit run.

@Rule
public TemporaryFolder folder = new TemporaryFolder();

folder.newFolder("NewFolder");

The new folder gets created at the path like below:

C:\Users\abc\abc\Local\Temp\junit991415299992369999\NewFolder

I'm looking for new folder to be created at the root, for example:

C:\NewFolder

How to achieve this which is good for both windows and linux?

I tried below

folder.newFolder("\\", "NewFolder"); // Same result
folder.newFolder("C:\\", "NewFolder"); // results in java.io.IOException: a folder with the name 'NewFolder' already exists. (Even though there is no existing folder with this name)
folder.newFolder("./", "NewFolder"); // creates at C:\Users\abc\abc\Local\Temp\junit991415299992369999\.\NewFolder

Thanks!

Upvotes: 0

Views: 1625

Answers (1)

Stefan Birkner
Stefan Birkner

Reputation: 24510

You have to create the TemporaryFolder with a different root. Something like

@Rule
public final TemporaryFolder folder = new TemporaryFolder(
    new File("C:\\")
);

Didn't test it on Windows but I think you get the idea.

Upvotes: 1

Related Questions