From 5d8023c996ec7fc0e438f8f66d55ed6c234bcd49 Mon Sep 17 00:00:00 2001 From: Niels Basjes Date: Mon, 1 Jun 2026 09:58:40 +0200 Subject: [PATCH] test: Reproduce System properties not restored after ParameterizedTest Signed-off-by: Niels Basjes --- .../util/SystemPropertiesExtensionTests.java | 104 +++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/jupiter-tests/src/test/java/org/junit/jupiter/api/util/SystemPropertiesExtensionTests.java b/jupiter-tests/src/test/java/org/junit/jupiter/api/util/SystemPropertiesExtensionTests.java index 31d7e3f22bbb..e81538903d44 100644 --- a/jupiter-tests/src/test/java/org/junit/jupiter/api/util/SystemPropertiesExtensionTests.java +++ b/jupiter-tests/src/test/java/org/junit/jupiter/api/util/SystemPropertiesExtensionTests.java @@ -44,6 +44,8 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.engine.AbstractJupiterTestEngineTests; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.junit.platform.testkit.engine.EngineExecutionResults; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoSettings; @@ -64,13 +66,25 @@ static void globalSetUp() { @AfterAll static void globalTearDown() { + // First check if all properties have been restored to their original values + assertThat(System.getProperty("A")).isEqualTo("old A"); + assertThat(System.getProperty("B")).isEqualTo("old B"); + assertThat(System.getProperty("C")).isEqualTo("old C"); + assertThat(System.getProperty("clear envvar D")).isNull(); + assertThat(System.getProperty("clear envvar E")).isNull(); + assertThat(System.getProperty("clear envvar F")).isNull(); + + // Cleanup System.clearProperty("A"); System.clearProperty("B"); System.clearProperty("C"); - assertThat(System.getProperty("clear prop D")).isNull(); - assertThat(System.getProperty("clear prop E")).isNull(); - assertThat(System.getProperty("clear prop F")).isNull(); + assertThat(System.getProperty("A")).isNull(); + assertThat(System.getProperty("B")).isNull(); + assertThat(System.getProperty("C")).isNull(); + assertThat(System.getProperty("clear envvar D")).isNull(); + assertThat(System.getProperty("clear envvar E")).isNull(); + assertThat(System.getProperty("clear envvar F")).isNull(); } @Nested @@ -678,6 +692,90 @@ void shouldInheritInNestedClass() { } + @Nested + @DisplayName("properties are restored after ParameterizedTest") + class RestoreAfterParameterizedTest { + + // This reproduces the system properties not being reset when set as part of a ParameterizedTest. + // These test all set only their 'own' system property and check ALL system properties. + // This will show in the test output which of the properties was not reset and thus give a clear + // indication which test actually did not work correctly. + // So the effect is that the BAD test does not fail, all the tests that are run AFTER this BAD test will fail. + // How many depends on the (random) order the tests are executed in. In any case the finish check will fail. + + private static final String PROP_NORMAL_TEST = "Normal Test PROP Variable"; + private static final String PROP_PARAM_TEST = "ParameterizedTest PROP Variable"; + private static final String PROP_PARAM_WITH_RESTORE_TEST = "ParameterizedTest With Restore PROP Variable"; + private static final String PROP_ORIG_VALUE = "Original Value"; + private static final String PROP_CHANGED_VALUE = "Changed Value"; + + // For these tests we need very explicit reporting about which variable is incorrect. + private static void assertSystemProperty(String propName, String propValue) { + assertThat(System.getProperty(propName)).as( + "System property \"" + propName + "\" should be \"" + propValue + "\".").isEqualTo(propValue); + } + + private static void assertSystemPropertyIsNull(String propName) { + assertThat(System.getProperty(propName)).as( + "System property \"" + propName + "\" should be NULL.").isNull(); + } + + @BeforeAll + public static void setup() { + System.setProperty(PROP_NORMAL_TEST, PROP_ORIG_VALUE); + System.setProperty(PROP_PARAM_TEST, PROP_ORIG_VALUE); + System.setProperty(PROP_PARAM_WITH_RESTORE_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_NORMAL_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_WITH_RESTORE_TEST, PROP_ORIG_VALUE); + } + + @AfterAll + public static void finish() { + assertSystemProperty(PROP_NORMAL_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_WITH_RESTORE_TEST, PROP_ORIG_VALUE); + + System.clearProperty(PROP_NORMAL_TEST); + System.clearProperty(PROP_PARAM_TEST); + System.clearProperty(PROP_PARAM_WITH_RESTORE_TEST); + + assertSystemPropertyIsNull(PROP_NORMAL_TEST); + assertSystemPropertyIsNull(PROP_PARAM_TEST); + assertSystemPropertyIsNull(PROP_PARAM_WITH_RESTORE_TEST); + } + + @Test + @SetSystemProperty(key = PROP_NORMAL_TEST, value = PROP_CHANGED_VALUE) + void testNormal() { + assertSystemProperty(PROP_NORMAL_TEST, PROP_CHANGED_VALUE); + assertSystemProperty(PROP_PARAM_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_WITH_RESTORE_TEST, PROP_ORIG_VALUE); + } + + @ParameterizedTest(name = "ParameterizedTest WITHOUT RestoreSystemProperties ({0})") + @ValueSource(ints = { 1 }) + @SetSystemProperty(key = PROP_PARAM_TEST, value = PROP_CHANGED_VALUE) + void testParamWithoutRestore(int dummy) { + assertThat(dummy).isEqualTo(1); + assertSystemProperty(PROP_NORMAL_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_TEST, PROP_CHANGED_VALUE); + assertSystemProperty(PROP_PARAM_WITH_RESTORE_TEST, PROP_ORIG_VALUE); + } + + @ParameterizedTest(name = "ParameterizedTest WITH RestoreSystemProperties ({0})") + @ValueSource(ints = { 1 }) + @SetSystemProperty(key = PROP_PARAM_WITH_RESTORE_TEST, value = PROP_CHANGED_VALUE) + @RestoreSystemProperties + void testParamWithRestore(int dummy) { + assertThat(dummy).isEqualTo(1); + assertSystemProperty(PROP_NORMAL_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_TEST, PROP_ORIG_VALUE); + assertSystemProperty(PROP_PARAM_WITH_RESTORE_TEST, PROP_CHANGED_VALUE); + } + + } + @ClearSystemProperty(key = "A") @ClearSystemProperty(key = "B") @SetSystemProperty(key = "clear prop D", value = "new D")