Skip to content

Commit d18bcb3

Browse files
committed
Raise MethodArgumentNotValidException consistently
Closes gh-30100
1 parent e17f5c5 commit d18bcb3

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAn
174174
}
175175
validateIfApplicable(binder, parameter);
176176
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
177-
throw new BindException(binder.getBindingResult());
177+
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
178178
}
179179
}
180180
// Value type adaptation, also covering java.util.Optional

spring-web/src/test/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessorTests.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import org.springframework.core.MethodParameter;
2929
import org.springframework.core.annotation.SynthesizingMethodParameter;
3030
import org.springframework.format.support.DefaultFormattingConversionService;
31-
import org.springframework.validation.BindException;
3231
import org.springframework.validation.BindingResult;
3332
import org.springframework.validation.Errors;
33+
import org.springframework.web.bind.MethodArgumentNotValidException;
3434
import org.springframework.web.bind.WebDataBinder;
3535
import org.springframework.web.bind.annotation.ModelAttribute;
3636
import org.springframework.web.bind.annotation.SessionAttributes;
@@ -109,7 +109,7 @@ public void setup() throws Exception {
109109

110110

111111
@Test
112-
public void supportedParameters() throws Exception {
112+
public void supportedParameters() {
113113
assertThat(this.processor.supportsParameter(this.paramNamedValidModelAttr)).isTrue();
114114
assertThat(this.processor.supportsParameter(this.paramModelAttr)).isTrue();
115115

@@ -119,8 +119,8 @@ public void supportedParameters() throws Exception {
119119
}
120120

121121
@Test
122-
public void supportedParametersInDefaultResolutionMode() throws Exception {
123-
processor = new ModelAttributeMethodProcessor(true);
122+
public void supportedParametersInDefaultResolutionMode() {
123+
this.processor = new ModelAttributeMethodProcessor(true);
124124

125125
// Only non-simple types, even if not annotated
126126
assertThat(this.processor.supportsParameter(this.paramNamedValidModelAttr)).isTrue();
@@ -132,21 +132,21 @@ public void supportedParametersInDefaultResolutionMode() throws Exception {
132132
}
133133

134134
@Test
135-
public void supportedReturnTypes() throws Exception {
136-
processor = new ModelAttributeMethodProcessor(false);
135+
public void supportedReturnTypes() {
136+
this.processor = new ModelAttributeMethodProcessor(false);
137137
assertThat(this.processor.supportsReturnType(returnParamNamedModelAttr)).isTrue();
138138
assertThat(this.processor.supportsReturnType(returnParamNonSimpleType)).isFalse();
139139
}
140140

141141
@Test
142-
public void supportedReturnTypesInDefaultResolutionMode() throws Exception {
143-
processor = new ModelAttributeMethodProcessor(true);
142+
public void supportedReturnTypesInDefaultResolutionMode() {
143+
this.processor = new ModelAttributeMethodProcessor(true);
144144
assertThat(this.processor.supportsReturnType(returnParamNamedModelAttr)).isTrue();
145145
assertThat(this.processor.supportsReturnType(returnParamNonSimpleType)).isTrue();
146146
}
147147

148148
@Test
149-
public void bindExceptionRequired() throws Exception {
149+
public void bindExceptionRequired() {
150150
assertThat(this.processor.isBindExceptionRequired(null, this.paramNonSimpleType)).isTrue();
151151
assertThat(this.processor.isBindExceptionRequired(null, this.paramNamedValidModelAttr)).isFalse();
152152
}
@@ -227,11 +227,13 @@ public void resolveArgumentBindException() throws Exception {
227227

228228
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
229229
dataBinder.getBindingResult().reject("error");
230+
230231
WebDataBinderFactory binderFactory = mock();
231232
given(binderFactory.createBinder(this.request, target, name)).willReturn(dataBinder);
232233

233-
assertThatExceptionOfType(BindException.class).isThrownBy(() ->
234+
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
234235
this.processor.resolveArgument(this.paramNonSimpleType, this.container, this.request, binderFactory));
236+
235237
verify(binderFactory).createBinder(this.request, target, name);
236238
}
237239

0 commit comments

Comments
 (0)