junit5
junit5 copied to clipboard
Provide support for setting a custom parent directory for @TempDir
My project requires that I do not write to /tmp
or whichever location it is when I use the TempDirectory
extension. So right now I have no choice but to still use junit-pioneer
's implementation, e.g. like this:
@RegisterExtension
Extension tempDirectory = TempDirectory.createInCustomDirectory(() -> Paths.get("build"));
@BeforeEach
void setup(@TempDir Path testProjectDir) {
// Do something with "testProjectDir".
}
But it would be nicer if I could just use stock JUnit 5 for this. JUnit 4 supported setting the parent dir, so I find the JUnit 5 way a bit too limiting.
Thanks in advance.
Tentatively slated for 5.6 M2 solely for the purpose of team discussion
If your project requires you not to write to the default location of temporary files, you should set the java.io.tmpdir
system property.
@rkrisztian Could you please try that?
Worked, thanks! I wish I knew about this. :)
Assigned to the general backlog and labeled as waiting for interest.
FYI: A co-worker of mine suggested the use of AbstractTask#getTemporaryDir()
:
tasks.withType(Test) {
systemProperty 'java.io.tmpdir', temporaryDir
}
I'm also interested in this functionality.
In my case I only want to affect the parent directory of one @TempDir
and therefore it does not make sense to use java.io.tmpdir
.
in my case junit always set it tempDir to the working directory of the project. This is annoying because it always generates a mess into my source code directories.
Additionally this mess is not deleted after the tests were executed. I mean in this case this is good, because I don't want my source code directories to be deleted. Do you think I forget to configure something, or is this function as intended?
@bes1002t, are you using the @TempDir
support in JUnit Jupiter?
If so, then the behavior you are describing sounds like a bug, and we would appreciate it if you would open a separate issue describing your setup and providing a failing example.
@sbrannen yes I'm using JUnit 5:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
If you don't see an issue here with my dependency declaration, I could create a new ticket with further explanation. :)
in my case junit always set it tempDir to the working directory of the project. This is annoying because it always generates a mess into my source code directories.
@bes1002t, it sounds like something else is setting the temp dir system property.
What is the output of System.out.println(System.getProperty("java.io.tmpdir"))
in your tests?
If it's the project's working directory, you'll need to figure out what is setting the java.io.tmpdir
JVM system property to that.