在JUnit 5中正确设置(系统)属性 [英] Properly set (system) properties in JUnit 5

查看:204
本文介绍了在JUnit 5中正确设置(系统)属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用类似于系统规则的方法来处理我们系统中的(系统)属性JUnit 4测试.这样做的主要原因是每次测试后都要清理环境,以便其他测试不会无意间依赖于可能的副作用.

We are using an approach similar to System Rules to handle (system) properties in our JUnit 4 tests. The main reason for this is to clean up the environment after each test, so that other tests do not inadvertently depend on possible side effects.

自从JUnit 5发布以来,我想知道是否有"JUnit 5方式"来做到这一点?

Since JUnit 5 is released, I wonder if there is a "JUnit 5 way" of doing this?

推荐答案

JUnit 5先锋,一个"JUnit 5扩展包".从版本开始:

There is JUnit 5 Pioneer, a "JUnit 5 extension pack". As of version:

<dependency>
    <groupId>org.junit-pioneer</groupId>
    <artifactId>junit-pioneer</artifactId>
    <version>0.5.1</version>
    <scope>test</scope>
</dependency>

@ClearSystemProperty@SetSystemProperty.从文档中:

@ClearSystemProperty@SetSystemProperty批注可以分别用于清除设置测试执行的系统属性的值.这两个注释均在测试方法级别上起作用,并且可重复且可组合.执行带注释的方法后,将恢复初始默认值.

The @ClearSystemProperty and @SetSystemProperty annotations can be used to clear, respectively, set the values of system properties for a test execution. Both annotations work on the test method level and are repeatable as well as combinable. After the annotated method has been executed, the initial default value is restored.

示例:

@Test
@ClearSystemProperty(key = "some key")
@SetSystemProperty(key = "another key", value = "new value")
void test() {
    assertNull(System.getProperty("some key"));
    assertEquals("new value", System.getProperty("another key"));
}

这篇关于在JUnit 5中正确设置(系统)属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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