junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

Support read / write of system properties during test execution

Open strangelookingnerd opened this issue 5 months ago • 2 comments

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.

strangelookingnerd avatar Jul 07 '25 06:07 strangelookingnerd

Team decision: We agree that this extension would be useful for many users of Jupiter. Therefore, let's include it in jupiter-api module in the org.junit.jupiter.api.util package. We will ask the original author(s) of the Pioneer version whether they want to contribute it.

marcphilipp avatar Oct 31 '25 14:10 marcphilipp

We at Pioneer are happy to contribute the extensions to JUnit. I will work on a PR after the one for #4727

Bukama avatar Nov 07 '25 17:11 Bukama