Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit4

maven

I have written a unit test that writes a file to the file-system, given no path it writes to the working directory; so if executed from the project directory it writes in the project root, if in the projects parent directory it writes to the parents root directory.

So what is the correct way to write to the target directory? Quite possibly a directory inside the target directory?

If I quite simply specify target/ with the file it will write to the parent projects target instead of the projects target.

UPDATE: I actually want the file after the test finishes. The file is for an extraction format for third-parties that needs to be sent to the third parties. The test can be switched on/off to allow me to only run if the format of the file changes for re-approval. It's not a huge problem where the file goes, but I would like something that's easy to find.

like image 857
Brett Ryan Avatar asked Sep 13 '12 05:09

Brett Ryan


People also ask

What do we used to run unit test with Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do I create a temp folder in JUnit?

public static class HasTempFolder { @Rule public TemporaryFolder folder= new TemporaryFolder(); @Test public void testUsingTempFolder() throws IOException { File createdFile= folder. newFile("myfile. txt"); File createdFolder= folder. newFolder("subfolder"); // ... } }

How do I read a JUnit file?

Read File and Resource in JUnit Test Examples Any file under src/test/resources is often copied to target/test-classes. To access these resource files in JUnit we can use the class's resource. It will locate the file in the test's classpath /target/test-classes.


1 Answers

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.

like image 85
maba Avatar answered Oct 06 '22 20:10

maba