diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java index 2ad27e7207..9956b32195 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java @@ -268,7 +268,7 @@ public class StandardConfigDataLocationResolver private Set resolvePatternEmptyDirectories(StandardConfigDataReference reference) { Resource[] subdirectories = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY); ConfigDataLocation location = reference.getConfigDataLocation(); - if (location.isOptional() && ObjectUtils.isEmpty(subdirectories)) { + if (!location.isOptional() && ObjectUtils.isEmpty(subdirectories)) { String message = String.format("Config data location '%s' contains no subdirectories", location); throw new ConfigDataLocationNotFoundException(location, message, null); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index d11631b0f5..7372d3627f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -715,7 +715,14 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { void runWhenMandatoryWildcardLocationHasNoSubdirectories() { assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy( () -> this.application.run("--spring.config.location=file:src/test/resources/config/0-empty/*/")) - .withMessage("Config data location 'file:src/test/resources/config/0-empty/*/' cannot be found"); + .withMessage( + "Config data location 'file:src/test/resources/config/0-empty/*/' contains no subdirectories"); + } + + @Test + void runWhenOptionalWildcardLocationHasNoSubdirectories() { + assertThatNoException().isThrownBy(() -> this.application + .run("--spring.config.location=optional:file:src/test/resources/config/0-empty/*/")); } @Test