简体   繁体   English

如何摆脱 Junit5 中的 TemporaryFolder 规则

[英]How to get rid of TemporaryFolder rule in Junit5

I'm migrating unit tests from Junit4 to Junit5.我正在将单元测试从 Junit4 迁移到 Junit5。 In the test I'm using TemporaryFolder rule from Junit4 API.在测试中,我使用来自 Junit4 API 的TemporaryFolder 规则 To keep the test working I added @EnableRuleMigrationSupport annotation:为了保持测试正常工作,我添加了@EnableRuleMigrationSupport注释:

@EnableRuleMigrationSupport
public final class SomeTest {

    @Rule
    public final TemporaryFolder tmp = new TemporaryFolder();

   // tests ...
}

As I understand, in Junit5 I need to use extensions instead of rules , but I can't find any replacement for TemporaryFolder in Junit5 extensions.据我了解,在 Junit5 中我需要使用extensions而不是rules ,但在 Junit5 扩展中我找不到任何替代TemporaryFolder的东西。 Does it exist?它存在吗? How to correctly replace TemporaryFolder rule with extension?如何正确用扩展名替换TemporaryFolder规则?

You can use the @TempDir annotation (JUnit 5.4+), described in §2.20.1 of the JUnit 5 User Guide .您可以使用JUnit 5 用户指南的 §2.20.1 中描述的@TempDir注释 (JUnit 5.4+)。 From the user guide ( emphasis mine):从用户指南(强调我的):

The built-in TempDirectory extension is used to create and clean up a temporary directory for an individual test or all tests in a test class.内置的TempDirectory扩展用于为单个测试或测试类中的所有测试创建和清理临时目录。 It is registered by default.默认情况下已注册。 To use it, annotate a non-private field of type java.nio.file.Path or java.io.File with @TempDir or add a parameter of type java.nio.file.Path or java.io.File annotated with @TempDir to a lifecycle method or test method.要使用它,请使用@TempDir注释java.nio.file.Pathjava.io.File类型的非私有字段,或添加使用@TempDir注释的java.nio.file.Pathjava.io.File类型的参数@TempDir到生命周期方法或测试方法。

Note: This extension was added in version 5.4 and is currently (as of 5.8.2 ) experimental.注意:此扩展是在5.4版中添加的,目前(从5.8.2开始)是实验性的。

Example of using an instance field:使用实例字段的示例:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path; // or use java.io.File

class SomeTests {

    @TempDir
    Path directory; // may be private since 5.8

}

Allow @TempDir fields to be private #2687 允许 @TempDir 字段为私有 #2687

Example of using a parameter of a test method:使用测试方法参数的示例:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path; // or use java.io.File

class SomeTests {

    @Test
    void testFoo(@TempDir Path directory) {
        // do test...
    }

}

Note: Constructor parameters are not supported.注意:不支持构造函数参数。

When the directory is created and deleted is described in the Javadoc of @TempDir (documentation quote from JUnit 5.8.2): @TempDir的 Javadoc 中描述了创建和删除目录的时间(来自 JUnit 5.8.2 的文档引用):

Creation创建

The temporary directory is only created if a field in a test class or a parameter in a lifecycle method or test method is annotated with @TempDir .仅当测试类中的字段或生命周期方法或测试方法中的参数使用@TempDir注释时,才会创建临时目录。 If the field type or parameter type is neither Path nor File or if the temporary directory cannot be created, an ExtensionConfigurationException or a ParameterResolutionException will be thrown as appropriate.如果字段类型或参数类型既不是Path也不是File ,或者如果无法创建临时目录,则会酌情抛出ExtensionConfigurationExceptionParameterResolutionException In addition, a ParameterResolutionException will be thrown for a constructor parameter annotated with @TempDir .此外,对于使用@TempDir注释的构造函数参数,将引发ParameterResolutionException

Scope范围

By default, a separate temporary directory is created for every declaration of the @TempDir annotation.默认情况下,为@TempDir注释的每个声明创建一个单独的临时目录。 If you want to share a temporary directory across all tests in a test class, you should declare the annotation on a static field or on a parameter of a @BeforeAll method.如果要在测试类中的所有测试之间共享临时目录,则应在static字段或@BeforeAll方法的参数上声明注释。

Old behavior旧行为

You can revert to the old behavior of using a single temporary directory by setting the junit.jupiter.tempdir.scope configuration parameter to per_context .您可以通过将junit.jupiter.tempdir.scope配置参数设置为per_context来恢复使用单个临时目录的旧行为。 In that case, the scope of the temporary directory depends on where the first @TempDir annotation is encountered when executing a test class.在这种情况下,临时目录的范围取决于在执行测试类时遇到第一个@TempDir注释的位置。 The temporary directory will be shared by all tests in a class when the annotation is present on a static field or on a parameter of a @BeforeAll method.当注释出现在static字段或@BeforeAll方法的参数上时,临时目录将由类中的所有测试共享。 Otherwise — for example, when @TempDir is only used on instance fields or on parameters in test, @BeforeEach , or @AfterEach methods — each test will use its own temporary directory.否则——例如,当@TempDir仅用于实例字段或 test、 @BeforeEach@AfterEach方法中的参数时——每个测试将使用自己的临时目录。

Deletion删除

When the end of the scope of a temporary directory is reached, ie when the test method or class has finished execution, JUnit will attempt to recursively delete all files and directories in the temporary directory and, finally, the temporary directory itself.当到达临时目录范围的末尾时,即当测试方法或类完成执行时,JUnit 将尝试递归删除临时目录中的所有文件和目录,最后是临时目录本身。 In case deletion of a file or directory fails, an IOException will be thrown that will cause the test or test class to fail.如果删除文件或目录失败,将抛出IOException导致测试或测试类失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM