在JUnit 5中处理系统属性

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值