diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java index 1c2ff18018..3a516ebe40 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -165,7 +165,20 @@ public final class TestPropertyValues { /** * Add the properties to the {@link System#getProperties() system properties} for the - * duration of the {@code call}, restoring previous values when the call completes. + * duration of the {@code action}, restoring previous values when it completes. + * @param action the action to take + * @since 3.0.0 + */ + public void applyToSystemProperties(Runnable action) { + applyToSystemProperties(() -> { + action.run(); + return null; + }); + } + + /** + * Add the properties to the {@link System#getProperties() system properties} for the + * duration of the {@code call}, restoring previous values when it completes. * @param the result type * @param call the call to make * @return the result of the call diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java index e007aa2f69..ed239713fc 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,13 +141,19 @@ class TestPropertyValuesTests { } @Test - void applyToSystemPropertiesShouldSetSystemProperties() { + void applyToSystemPropertiesWithCallableShouldSetSystemProperties() { TestPropertyValues.of("foo=bar").applyToSystemProperties(() -> { assertThat(System.getProperty("foo")).isEqualTo("bar"); return null; }); } + @Test + void applyToSystemPropertiesWithRunnableShouldSetSystemProperties() { + TestPropertyValues.of("foo=bar") + .applyToSystemProperties(() -> assertThat(System.getProperty("foo")).isEqualTo("bar")); + } + @Test void applyToSystemPropertiesShouldRestoreSystemProperties() { System.setProperty("foo", "bar1");