mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Cope with different exception messages in ValidationException failure analyzer
Fixes gh-19671
This commit is contained in:
parent
101fd14286
commit
4a76bc8b46
@ -30,12 +30,16 @@ import org.springframework.boot.diagnostics.FailureAnalyzer;
|
||||
*/
|
||||
class ValidationExceptionFailureAnalyzer extends AbstractFailureAnalyzer<ValidationException> {
|
||||
|
||||
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);
|
||||
|
@ -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() {
|
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user