|
24 | 24 | import jakarta.validation.constraints.Size;
|
25 | 25 | import org.junit.jupiter.api.Test;
|
26 | 26 |
|
| 27 | +import org.springframework.context.i18n.LocaleContextHolder; |
27 | 28 | import org.springframework.context.support.StaticMessageSource;
|
28 | 29 | import org.springframework.core.MethodParameter;
|
29 | 30 | import org.springframework.validation.BeanPropertyBindingResult;
|
|
36 | 37 |
|
37 | 38 | /**
|
38 | 39 | * Unit tests for {@link MethodArgumentNotValidException}.
|
| 40 | + * |
39 | 41 | * @author Rossen Stoyanchev
|
40 | 42 | */
|
41 | 43 | public class MethodArgumentNotValidExceptionTests {
|
@@ -68,17 +70,23 @@ void errorsToStringListWithMessageSource() throws Exception {
|
68 | 70 | }
|
69 | 71 |
|
70 | 72 | private static MethodArgumentNotValidException createException(Person person) throws Exception {
|
71 |
| - LocalValidatorFactoryBean validatorBean = new LocalValidatorFactoryBean(); |
72 |
| - validatorBean.afterPropertiesSet(); |
73 |
| - SpringValidatorAdapter validator = new SpringValidatorAdapter(validatorBean); |
| 73 | + LocaleContextHolder.setLocale(Locale.UK); |
| 74 | + try { |
| 75 | + LocalValidatorFactoryBean validatorBean = new LocalValidatorFactoryBean(); |
| 76 | + validatorBean.afterPropertiesSet(); |
| 77 | + SpringValidatorAdapter validator = new SpringValidatorAdapter(validatorBean); |
74 | 78 |
|
75 |
| - BindingResult result = new BeanPropertyBindingResult(person, "person"); |
76 |
| - validator.validate(person, result); |
| 79 | + BindingResult result = new BeanPropertyBindingResult(person, "person"); |
| 80 | + validator.validate(person, result); |
77 | 81 |
|
78 |
| - Method method = Handler.class.getDeclaredMethod("handle", Person.class); |
79 |
| - MethodParameter parameter = new MethodParameter(method, 0); |
| 82 | + Method method = Handler.class.getDeclaredMethod("handle", Person.class); |
| 83 | + MethodParameter parameter = new MethodParameter(method, 0); |
80 | 84 |
|
81 |
| - return new MethodArgumentNotValidException(parameter, result); |
| 85 | + return new MethodArgumentNotValidException(parameter, result); |
| 86 | + } |
| 87 | + finally { |
| 88 | + LocaleContextHolder.resetLocaleContext(); |
| 89 | + } |
82 | 90 | }
|
83 | 91 |
|
84 | 92 |
|
|
0 commit comments