What is the correct way to write to temp file during unit tests with Maven?

You will want to be able to run your tests both from an IDE as well as from Maven, so best practice is to write your tests so that they do not assume they are being run within Maven.

One of the best ways to deal with temporary files is using junit rules. This allows you to rely on the rule to clean up for you.


You could try to use TemporaryFolder JUnit @Rule as described here

The TemporaryFolder creates a folder in the default temporary file directory specified by the system property java.io.tmpdir. The method newFile creates a new file in the temporary directory and newFolder creates a new folder.

When the test method finishes, JUnit automatically deletes all files and directories in and including the TemporaryFolder. JUnit guarantees to delete the resources, whether the test passes or fails.


After Question Updated

You can change the working directory used by maven-surefire-plugin.

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.3</version>
        <configuration>
          <workingDirectory>${project.build.directory}</workingDirectory>
        </configuration>
      </plugin>
    [...]
</plugins>

You can change that working directory to anything you need for your tests like ${project.build.directory}/my_special_dir/.

The working directory in surefire plugin only affects tests being run and ONLY for tests being conducted by maven. If you run your tests from within an IDE the working directory will be something else.


No need to reinvent the wheel...

The JDK provides a way to a create temporary file, and a way to automatically delete it on exit:

File file = File.createTempFile( "some-prefix", "some-ext");
file.deleteOnExit();

Use the file and it will be deleted automatically when your test finishes. That's all there is to it.

To specify the directory to use for temporary files, use the overloaded method:

File file = File.createTempFile( "prefix", "ext", new File("/some/dir/path"));

I would write a routine which determines where the file should be written to, which i unitest afterwards, in general i try to avoid (as possible) accessing persistent data in unittests, like file IO or database access, this has performance reasons and others too.

Take a look at this asnwer: https://stackoverflow.com/a/8032504/395659

Tags:

Java

Maven

Junit4