diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzer.java index 5afcd70ef9..fff7c1f30a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzer.java @@ -30,12 +30,16 @@ import org.springframework.boot.diagnostics.FailureAnalyzer; */ class ValidationExceptionFailureAnalyzer extends AbstractFailureAnalyzer { - private static final String MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a " + private static final String JAVAX_MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a " + "Configuration, because no Bean Validation provider could be found"; + private static final String JAKARTA_MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a " + + "Configuration, because no Jakarta Bean Validation provider could be found"; + @Override protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) { - if (cause.getMessage().startsWith(MISSING_IMPLEMENTATION_MESSAGE)) { + if (cause.getMessage().startsWith(JAVAX_MISSING_IMPLEMENTATION_MESSAGE) + || cause.getMessage().startsWith(JAKARTA_MISSING_IMPLEMENTATION_MESSAGE)) { return new FailureAnalysis( "The Bean Validation API is on the classpath but no implementation could be found", "Add an implementation, such as Hibernate Validator, to the classpath", cause); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/JakartaApiValidationExceptionFailureAnalyzerTests.java similarity index 97% rename from spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java rename to spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/JakartaApiValidationExceptionFailureAnalyzerTests.java index d8ca5c7488..117ebf6c48 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/JakartaApiValidationExceptionFailureAnalyzerTests.java @@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Andy Wilkinson */ @ClassPathExclusions("hibernate-validator-*.jar") -class ValidationExceptionFailureAnalyzerTests { +class JakartaApiValidationExceptionFailureAnalyzerTests { @Test void validatedPropertiesTest() { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/JavaxApiValidationExceptionFailureAnalyzerTests2.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/JavaxApiValidationExceptionFailureAnalyzerTests2.java new file mode 100644 index 0000000000..048d21e4fd --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/JavaxApiValidationExceptionFailureAnalyzerTests2.java @@ -0,0 +1,79 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.diagnostics.analyzer; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; +import org.springframework.boot.testsupport.classpath.ClassPathOverrides; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.validation.annotation.Validated; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests for {@link ValidationExceptionFailureAnalyzer} + * + * @author Andy Wilkinson + */ +@ClassPathExclusions("hibernate-validator-*.jar") +@ClassPathOverrides("javax.validation:validation-api:2.0.1.Final") +class JavaxApiValidationExceptionFailureAnalyzerTests2 { + + @Test + void validatedPropertiesTest() { + assertThatExceptionOfType(Exception.class) + .isThrownBy(() -> new AnnotationConfigApplicationContext(TestConfiguration.class).close()) + .satisfies((ex) -> assertThat(new ValidationExceptionFailureAnalyzer().analyze(ex)).isNotNull()); + } + + @Test + void nonValidatedPropertiesTest() { + new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class).close(); + } + + @EnableConfigurationProperties(TestProperties.class) + static class TestConfiguration { + + TestConfiguration(TestProperties testProperties) { + } + + } + + @ConfigurationProperties("test") + @Validated + static class TestProperties { + + } + + @EnableConfigurationProperties(NonValidatedTestProperties.class) + static class NonValidatedTestConfiguration { + + NonValidatedTestConfiguration(NonValidatedTestProperties testProperties) { + } + + } + + @ConfigurationProperties("test") + static class NonValidatedTestProperties { + + } + +}