本文介绍了在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
.从文档中:
示例:
@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中正确设置(系统)属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!