diff --git a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java index 40f6c709342..bdb3c117705 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java @@ -393,9 +393,7 @@ public abstract class TestPropertySourceUtils { public static Map convertInlinedPropertiesToMap(String... inlinedProperties) { Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null"); - LinkedHashMap map = new LinkedHashMap<>(); - SequencedProperties sequencedProperties = new SequencedProperties(map); - + SequencedProperties sequencedProperties = new SequencedProperties(); for (String input : inlinedProperties) { if (!StringUtils.hasText(input)) { continue; @@ -407,8 +405,7 @@ public abstract class TestPropertySourceUtils { throw new IllegalStateException("Failed to load test environment properties from [" + input + "]", ex); } } - - return map; + return sequencedProperties.getSequencedMap(); } private static List>> findRepeatableAnnotations( @@ -452,26 +449,28 @@ public abstract class TestPropertySourceUtils { } /** - * Extension of {@link Properties} that mimics a {@code SequencedMap} by - * tracking all added properties in the supplied {@link LinkedHashMap}. + * Extension of {@link Properties} that mimics a {@code SequencedMap} by tracking + * all added properties with a {@link String} key in a {@link LinkedHashMap}. * @since 6.1 */ @SuppressWarnings("serial") private static class SequencedProperties extends Properties { - private final LinkedHashMap map; - - SequencedProperties(LinkedHashMap map) { - this.map = map; - } + private final LinkedHashMap map = new LinkedHashMap<>(); @Override - public synchronized Object put(Object key, Object value) { + public Object put(Object key, Object value) { if (key instanceof String str) { - this.map.put(str, value); + return this.map.put(str, value); } - return super.put(key, value); + // No need to invoke super.put(key, value); + return null; } + + public Map getSequencedMap() { + return this.map; + } + } }