Skip to content

Commit 768aa5d

Browse files
committed
Ignore exception if ValidationAdapter can't get a MessageInterpolator
Fixes gh-16177
1 parent c592e71 commit 768aa5d

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.validation;
1818

19+
import javax.validation.ValidationException;
20+
1921
import org.springframework.beans.BeansException;
2022
import org.springframework.beans.factory.DisposableBean;
2123
import org.springframework.beans.factory.InitializingBean;
@@ -135,7 +137,13 @@ private static Validator getExisting(ApplicationContext applicationContext) {
135137

136138
private static Validator create() {
137139
OptionalValidatorFactoryBean validator = new OptionalValidatorFactoryBean();
138-
validator.setMessageInterpolator(new MessageInterpolatorFactory().getObject());
140+
try {
141+
validator
142+
.setMessageInterpolator(new MessageInterpolatorFactory().getObject());
143+
}
144+
catch (ValidationException ex) {
145+
// Ignore
146+
}
139147
return wrap(validator, false);
140148
}
141149

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapterTests.java

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121
import javax.validation.constraints.Min;
2222

23-
import org.junit.After;
2423
import org.junit.Test;
2524

25+
import org.springframework.boot.test.context.FilteredClassLoader;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2627
import org.springframework.context.ApplicationContext;
27-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.io.ClassPathResource;
3031
import org.springframework.validation.MapBindingResult;
3132
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
3233

@@ -41,60 +42,65 @@
4142
* Tests for {@link ValidatorAdapter}.
4243
*
4344
* @author Stephane Nicoll
45+
* @author Madhura Bhave
4446
*/
4547
public class ValidatorAdapterTests {
4648

47-
private AnnotationConfigApplicationContext context;
48-
49-
@After
50-
public void close() {
51-
if (this.context != null) {
52-
this.context.close();
53-
}
54-
}
49+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner();
5550

5651
@Test
5752
public void wrapLocalValidatorFactoryBean() {
58-
ValidatorAdapter wrapper = load(LocalValidatorFactoryBeanConfig.class);
59-
assertThat(wrapper.supports(SampleData.class)).isTrue();
60-
MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(),
61-
"test");
62-
wrapper.validate(new SampleData(40), errors);
63-
assertThat(errors.getErrorCount()).isEqualTo(1);
53+
this.contextRunner.withUserConfiguration(LocalValidatorFactoryBeanConfig.class)
54+
.run((context) -> {
55+
ValidatorAdapter wrapper = context.getBean(ValidatorAdapter.class);
56+
assertThat(wrapper.supports(SampleData.class)).isTrue();
57+
MapBindingResult errors = new MapBindingResult(
58+
new HashMap<String, Object>(), "test");
59+
wrapper.validate(new SampleData(40), errors);
60+
assertThat(errors.getErrorCount()).isEqualTo(1);
61+
});
6462
}
6563

6664
@Test
6765
public void wrapperInvokesCallbackOnNonManagedBean() {
68-
load(NonManagedBeanConfig.class);
69-
LocalValidatorFactoryBean validator = this.context
70-
.getBean(NonManagedBeanConfig.class).validator;
71-
verify(validator, times(1)).setApplicationContext(any(ApplicationContext.class));
72-
verify(validator, times(1)).afterPropertiesSet();
73-
verify(validator, never()).destroy();
74-
this.context.close();
75-
this.context = null;
76-
verify(validator, times(1)).destroy();
66+
this.contextRunner.withUserConfiguration(NonManagedBeanConfig.class)
67+
.run((context) -> {
68+
LocalValidatorFactoryBean validator = context
69+
.getBean(NonManagedBeanConfig.class).validator;
70+
verify(validator, times(1))
71+
.setApplicationContext(any(ApplicationContext.class));
72+
verify(validator, times(1)).afterPropertiesSet();
73+
verify(validator, never()).destroy();
74+
context.close();
75+
verify(validator, times(1)).destroy();
76+
});
7777
}
7878

7979
@Test
8080
public void wrapperDoesNotInvokeCallbackOnManagedBean() {
81-
load(ManagedBeanConfig.class);
82-
LocalValidatorFactoryBean validator = this.context
83-
.getBean(ManagedBeanConfig.class).validator;
84-
verify(validator, never()).setApplicationContext(any(ApplicationContext.class));
85-
verify(validator, never()).afterPropertiesSet();
86-
verify(validator, never()).destroy();
87-
this.context.close();
88-
this.context = null;
89-
verify(validator, never()).destroy();
81+
this.contextRunner.withUserConfiguration(ManagedBeanConfig.class)
82+
.run((context) -> {
83+
LocalValidatorFactoryBean validator = context
84+
.getBean(ManagedBeanConfig.class).validator;
85+
verify(validator, never())
86+
.setApplicationContext(any(ApplicationContext.class));
87+
verify(validator, never()).afterPropertiesSet();
88+
verify(validator, never()).destroy();
89+
context.close();
90+
verify(validator, never()).destroy();
91+
});
9092
}
9193

92-
private ValidatorAdapter load(Class<?> config) {
93-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
94-
ctx.register(config);
95-
ctx.refresh();
96-
this.context = ctx;
97-
return this.context.getBean(ValidatorAdapter.class);
94+
@Test
95+
public void wrapperWhenValidationProviderNotPresentShouldNotThrowException() {
96+
ClassPathResource hibernateValidator = new ClassPathResource(
97+
"META-INF/services/javax.validation.spi.ValidationProvider");
98+
this.contextRunner
99+
.withClassLoader(new FilteredClassLoader(
100+
FilteredClassLoader.ClassPathResourceFilter
101+
.of(hibernateValidator),
102+
FilteredClassLoader.PackageFilter.of("org.hibernate.validator")))
103+
.run((context) -> ValidatorAdapter.get(context, null));
98104
}
99105

100106
@Configuration

0 commit comments

Comments
 (0)