Skip to content

Support read / write of system properties during test execution #4726

@strangelookingnerd

Description

@strangelookingnerd

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions