1

I am using junit's TemporaryFolder to test part of my application that deals with the filesystem. Is it possible to set the junit variable java.io.tmpdir to an Environment variable in the run configuration?

yellavon
  • 2,821
  • 12
  • 52
  • 66
  • 5
    If you have an external dependency: extract it into a separate service so you can just mock it out when needed. Just like any other dependency. – Jeroen Vannevel May 14 '14 at 15:50
  • Ok, but the question remains, how can I set the java.io.tmpdir variable to the environment variable? – yellavon May 14 '14 at 16:15
  • It's much better after your edit. Still I'm not sure what you want. Place there some code - your tested method and your unit test attempt and some more explanation. – Honza Zidek May 14 '14 at 16:52
  • @HonzaZidek thanks for your help but the question does not require code examples to understand what I am asking and any code I put in here would be unrelated to the question asked. If you do not understand the question now, I believe it just means you won't be able to provide an answer. – yellavon May 14 '14 at 17:05
  • Yes, that's what I'm talking about. – yellavon May 14 '14 at 19:08
  • And what do you mean by "junit variable java.io.tmpdir"? As far as I know, the variable java.io.tmpdir is a JVM system property, not a junit variable. – Honza Zidek May 14 '14 at 19:17
  • Sorry, that just a typo. Fixed now. So the proper way to access java.io.tmpdir is `System.getenv("java.io.tmpdir")` ? – yellavon May 14 '14 at 20:29
  • So finally it seems that your question is: "How to configure JUnit so it uses a specific folder for creating the temporary files?" :) If so, please re-word the question and its title, if not, explain it more please. – Honza Zidek May 15 '14 at 12:00

1 Answers1

3

Based on our long-lasting discussion I now understand that you ask this:

How to configure JUnit so it uses a specific folder for creating the temporary files.

(If I get your question properly, please edit it accordingly and change the title.)

JUnit 4.11+

The easiest way is to use the new constructor TemporaryFolder(File parentFolder). This is the preferred way.

JUnit < 4.11

You are aware that the org.junit.rules.TemporaryFolder uses the the system property java.io.tmpdir. Actually, when you look at the source code, it uses internally the File.createTempFile(prefix, suffix) method which uses this system property.

You will find exhaustive information here: Environment variable to control java.io.tmpdir?.

More generally about system properties and environment variables: Java system properties and environment variables

However, you should notice this sentence from the Java-doc:

A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the temporary directory used by this method.

If you want to be sure you have the temporary folder always under your control, you may create your own version of org.junit.rules.TemporaryFolder - it's not so difficult, you just use the File.createTempFile(prefix, suffix, directory) with explicitly assigned the 3rd parameter directory.

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118