Skip to content

Solved: Temporary File Flood in JUnit Tests in Maven Build

by on September 13, 2013

Did you ever stumble across unit tests which flood your temporary folder with a lot of temporary files? Perhaps even observing disk space problems because of this? This post will provide a Quick Fix for tests triggered during Maven Build.

Problem

Most tests requiring temporary files rely on java.io.File.createTempFile() to create them. This will create a temporary file at a location defined by the system property java.io.tmpdir which for example on Linux system points to /tmp.

If tests (or their authors) would have been nice they had at least marked the file to be deleted on exit of JVM. But as experience shows (having currently tests at hand creating 70 MB of temporary files) not everyone is aware of this testiquette.

Approaching

The best solution would be to refactor the tests and to force them to use the JUnit Rule TemporaryFolder instead. Gary Gregory has greatly described this in his post JUnit Tip: Use rules to manage temporary files and folders, 2010-01-20. It is also capable of not only creating temporary files but also folders.

But if you have masses of tests to adopt this is not appropriate – and it may not be the solution for all of your problems if it is not only the test creating temporary files but also the software under test (SUT).

To detect which files get created in the temporary folder the Linux tool iwatch was helpful to me. Through the following command line I could easily see which files got created:

$ iwatch -r -e create /tmp

If you are building your workspace through Apache Maven you most likely want to have the temporary files been placed into the target/ folder as this is also the folder which gets cleaned upon mvn clean and is most likely also excluded from being commited to your version control system (VCS).

Solution

Maven runs the tests through the Maven Surefire Plugin. This can be configured to pass system properties to the JVM executing the tests. And you already might have guessed it: You can also set the property java.io.tmpdir in here. My solution goes a little further: I also set the working directory for tests as some SUT and some tests also tend to write to the current directory.

Here is a template for how to configure the Surefire Plugin in your plugin management section:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.16</version>
  <configuration>
  <!-- ... -->
    <workingDirectory>${project.build.directory}</workingDirectory>
    <systemPropertyVariables>
      <java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
    </systemPropertyVariables>
  </configuration>
</plugin>

See Also

From → Dev, Testing

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s