stefanbirkner / system-rules

A collection of JUnit rules for testing code which uses java.lang.System.
http://stefanbirkner.github.io/system-rules
Other
546 stars 71 forks source link

RestoreSystemProperties Rule does not accurately preserve Properties #43

Closed shollander closed 8 years ago

shollander commented 8 years ago

I have encountered a problem with the RestoreSystemProperties Rule. It modifies the original system properties and causes some of my unit tests to fail. I have done a little debugging and the problem is that the before() method in RestoreSystemProperties.java actually changes the existing properties. It appears that it is trying to create a copy, but it actually does not. If you look at the javadoc for Properties, the single argument constructor does not copy the Properties passed in. Rather it uses those as defaults when properties have not been set. This has different behavior than having the properties set. One example of this is when calling contains() the Property object will return false if the property has not been set - even if there is a default for that key.

I would recommend a simple fix:

    protected void before() throws Throwable {
        originalProperties = new Properties();
        originalProperties.pullAll(getProperties());
}
stefanbirkner commented 8 years ago

The bug has been fixed and the fix is available in release 1.16.1.