Description
Code under test is oftentimes controlled by system properties. This usually leads to boilerplate code such as
private String property;
@BeforeEach
void setUp() {
property = System.setProperty("key", "value");
}
@AfterEach
void tearDown() {
if (property != null) {
System.setProperty("key", property);
} else {
System.clearProperty("key");
}
}
An annotation-based solution would remove the need for such boilerplate code and allow a cleaner code style and thus improve readability. The annotation should be supported on both, class and method level. Further, in order to address concurrency issues ResourceLock
should be utilized to allow safe manipulation of system properties when tests run in parallel.
There is an existing solution in junit-pioneer
. https://junit-pioneer.org/docs/system-properties/ contains more examples for potential use cases.
That being said, while there is a 3rd-party library, I still think that this is a feature that should be included in JUnit already.
I'd be happy to prepare a pull request for this in case there is positve feedback for this feature.