Skip to content

Commit 4a76bc8

Browse files
committed
Cope with different exception messages in ValidationException failure analyzer
Fixes gh-19671
1 parent 101fd14 commit 4a76bc8

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030
*/
3131
class ValidationExceptionFailureAnalyzer extends AbstractFailureAnalyzer<ValidationException> {
3232

33-
private static final String MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a "
33+
private static final String JAVAX_MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a "
3434
+ "Configuration, because no Bean Validation provider could be found";
3535

36+
private static final String JAKARTA_MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a "
37+
+ "Configuration, because no Jakarta Bean Validation provider could be found";
38+
3639
@Override
3740
protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) {
38-
if (cause.getMessage().startsWith(MISSING_IMPLEMENTATION_MESSAGE)) {
41+
if (cause.getMessage().startsWith(JAVAX_MISSING_IMPLEMENTATION_MESSAGE)
42+
|| cause.getMessage().startsWith(JAKARTA_MISSING_IMPLEMENTATION_MESSAGE)) {
3943
return new FailureAnalysis(
4044
"The Bean Validation API is on the classpath but no implementation could be found",
4145
"Add an implementation, such as Hibernate Validator, to the classpath", cause);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @author Andy Wilkinson
3434
*/
3535
@ClassPathExclusions("hibernate-validator-*.jar")
36-
class ValidationExceptionFailureAnalyzerTests {
36+
class JakartaApiValidationExceptionFailureAnalyzerTests {
3737

3838
@Test
3939
void validatedPropertiesTest() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.diagnostics.analyzer;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
23+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
24+
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.validation.annotation.Validated;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
30+
31+
/**
32+
* Tests for {@link ValidationExceptionFailureAnalyzer}
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
@ClassPathExclusions("hibernate-validator-*.jar")
37+
@ClassPathOverrides("javax.validation:validation-api:2.0.1.Final")
38+
class JavaxApiValidationExceptionFailureAnalyzerTests2 {
39+
40+
@Test
41+
void validatedPropertiesTest() {
42+
assertThatExceptionOfType(Exception.class)
43+
.isThrownBy(() -> new AnnotationConfigApplicationContext(TestConfiguration.class).close())
44+
.satisfies((ex) -> assertThat(new ValidationExceptionFailureAnalyzer().analyze(ex)).isNotNull());
45+
}
46+
47+
@Test
48+
void nonValidatedPropertiesTest() {
49+
new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class).close();
50+
}
51+
52+
@EnableConfigurationProperties(TestProperties.class)
53+
static class TestConfiguration {
54+
55+
TestConfiguration(TestProperties testProperties) {
56+
}
57+
58+
}
59+
60+
@ConfigurationProperties("test")
61+
@Validated
62+
static class TestProperties {
63+
64+
}
65+
66+
@EnableConfigurationProperties(NonValidatedTestProperties.class)
67+
static class NonValidatedTestConfiguration {
68+
69+
NonValidatedTestConfiguration(NonValidatedTestProperties testProperties) {
70+
}
71+
72+
}
73+
74+
@ConfigurationProperties("test")
75+
static class NonValidatedTestProperties {
76+
77+
}
78+
79+
}

0 commit comments

Comments
 (0)