BATCH-1627: add BindException to ValidationException in SpringValidator
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.Collection;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
|
||||
/**
|
||||
@@ -49,7 +50,7 @@ public class SpringValidator<T> implements Validator<T>, InitializingBean {
|
||||
validator.validate(item, errors);
|
||||
|
||||
if (errors.hasErrors()) {
|
||||
throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors));
|
||||
throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors), new BindException(errors));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
@@ -72,6 +73,19 @@ public class SpringValidatorTests {
|
||||
validator.validate(MockSpringValidator.REJECT_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Typical failed validation - {@link ValidationException} is thrown
|
||||
*/
|
||||
@Test(expected = BindException.class)
|
||||
public void testValidateFailureWithErrors() throws Exception {
|
||||
try {
|
||||
validator.validate(MockSpringValidator.REJECT_VALUE);
|
||||
}
|
||||
catch (ValidationException e) {
|
||||
throw (BindException) e.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Typical failed validation - message contains the item and names of
|
||||
* invalid fields.
|
||||
|
||||
Reference in New Issue
Block a user