In Java, properties are configuration values that are represented as key-value pairs, usually managed inside a Properties
object. System properties are basically the Properties
object of the System
class, which "describes the configuration of the current working environment". This includes information about the current user, the OS or the Java runtime. The System
class offers various methods to interact with its properties, however, the ones most used are:
- clearProperty(字符串键):删除指定键指示的系统属性。getProperty(字符串键):获取由指定键指示的系统属性。setProperty(字符串键,字符串值):设置指定键指示的系统属性。
有时在测试以某种方式使用的代码时需要控制系统属性系统。 这样做时,您不仅应该疯狂地改变其值,否则测试可能会无意中取决于可能的副作用。 然后,更改测试代码,甚至更改执行顺序,都可能导致难以调试的测试失败。 因此,您希望有一个适当的还原机制来在每次测试后清理环境。
When it comes to JUnit 4, there are already off-the-shelf libraries for this such as the fantastic System Rules project. But for JUnit 5, you had to come up with a custom solution—at least until now.
There is JUnit Pioneer, a semi-official extension pack for JUnit 5. It offers various neat extensions, and as of version 0.5.0 also a mechanism to