在使用Maven进行单元测试期间写入临时文件的正确方法是什么? [英] What is the correct way to write to temp file during unit tests with Maven?

查看:253
本文介绍了在使用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?

如果我只是简单地用文件指定 target / 它会写到父项目目标而不是项目目标。

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.

推荐答案

你可以尝试使用 TemporaryFolder JUnit @Rule描述这里

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


TemporaryFolder 在系统属性java.io.tmpdir指定的默认临时文件目录中创建一个文件夹。 newFile方法在临时目录中创建一个新文件,newFolder创建一个新文件夹。

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.

当测试方法完成时,JUnit会自动删除所有文件和目录,包括临时文件夹。 JUnit保证删除资源,无论测试通过还是失败。

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.






问题更新后

您可以更改 maven-surefire-plugin

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>

您可以将该工作目录更改为测试所需的任何内容,例如 $ {project.build.directory} / my_special_dir /

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

surefire插件中的工作目录仅影响正在运行的测试,仅用于正在进行的测试由maven。如果从IDE中运行测试,则工作目录将是其他内容。

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.

这篇关于在使用Maven进行单元测试期间写入临时文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆