Consistent Locale exposure for Bean Validation message assertions
See gh-29825 See gh-30198
This commit is contained in:
parent
bbf3c6ecac
commit
93345de687
|
|
@ -24,6 +24,7 @@ import jakarta.validation.constraints.Min;
|
|||
import jakarta.validation.constraints.Size;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.context.support.StaticMessageSource;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
|
|
@ -36,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
|
||||
/**
|
||||
* Unit tests for {@link MethodArgumentNotValidException}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MethodArgumentNotValidExceptionTests {
|
||||
|
|
@ -68,17 +70,23 @@ public class MethodArgumentNotValidExceptionTests {
|
|||
}
|
||||
|
||||
private static MethodArgumentNotValidException createException(Person person) throws Exception {
|
||||
LocalValidatorFactoryBean validatorBean = new LocalValidatorFactoryBean();
|
||||
validatorBean.afterPropertiesSet();
|
||||
SpringValidatorAdapter validator = new SpringValidatorAdapter(validatorBean);
|
||||
LocaleContextHolder.setLocale(Locale.UK);
|
||||
try {
|
||||
LocalValidatorFactoryBean validatorBean = new LocalValidatorFactoryBean();
|
||||
validatorBean.afterPropertiesSet();
|
||||
SpringValidatorAdapter validator = new SpringValidatorAdapter(validatorBean);
|
||||
|
||||
BindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
BindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
|
||||
Method method = Handler.class.getDeclaredMethod("handle", Person.class);
|
||||
MethodParameter parameter = new MethodParameter(method, 0);
|
||||
Method method = Handler.class.getDeclaredMethod("handle", Person.class);
|
||||
MethodParameter parameter = new MethodParameter(method, 0);
|
||||
|
||||
return new MethodArgumentNotValidException(parameter, result);
|
||||
return new MethodArgumentNotValidException(parameter, result);
|
||||
}
|
||||
finally {
|
||||
LocaleContextHolder.resetLocaleContext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.reactive.result.method.annotation;
|
|||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
|
@ -27,12 +28,14 @@ import jakarta.validation.Valid;
|
|||
import jakarta.validation.constraints.Size;
|
||||
import jakarta.validation.executable.ExecutableValidator;
|
||||
import jakarta.validation.metadata.BeanDescriptor;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.context.MessageSourceResolvable;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.validation.Errors;
|
||||
|
|
@ -63,12 +66,14 @@ import static org.mockito.Mockito.mock;
|
|||
|
||||
/**
|
||||
* Method validation tests for Spring MVC controller methods.
|
||||
*
|
||||
* <p>When adding tests, consider the following others:
|
||||
* <ul>
|
||||
* <li>{@code HandlerMethodTests} -- detection if methods need validation
|
||||
* <li>{@code MethodValidationAdapterTests} -- method validation independent of Spring MVC
|
||||
* <li>{@code MethodValidationProxyTests} -- method validation with proxy scenarios
|
||||
* </ul>
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MethodValidationTests {
|
||||
|
|
@ -85,6 +90,8 @@ public class MethodValidationTests {
|
|||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
LocaleContextHolder.setDefaultLocale(Locale.UK);
|
||||
|
||||
LocalValidatorFactoryBean validatorBean = new LocalValidatorFactoryBean();
|
||||
validatorBean.afterPropertiesSet();
|
||||
this.jakartaValidator = new InvocationCountingValidator(validatorBean);
|
||||
|
|
@ -105,6 +112,11 @@ public class MethodValidationTests {
|
|||
return handlerAdapter;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void reset() {
|
||||
LocaleContextHolder.setDefaultLocale(null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void modelAttribute() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue