@Override public Trade process(Trade item) throws Exception { if ((failedItem == null && index++ == failure) || (failedItem != null && failedItem.equals(item))) { failedItem = item; throw new ValidationException("Some bad data for " + failedItem); } return item; } }
/** * 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. */ @Test public void testValidateFailureWithFields() { try { validator.validate(MockSpringValidator.REJECT_MULTI_VALUE); fail("exception should have been thrown on invalid value"); } catch (ValidationException expected) { assertTrue("message should contain the item#toString() value", expected.getMessage().contains( "TestBeanToString")); assertTrue("message should contain names of the invalid fields", expected.getMessage().contains("foo")); assertTrue("message should contain names of the invalid fields", expected.getMessage().contains("bar")); } }
@Override public Exception getException(String msg) throws Exception { return new ValidationException(msg); }
@Override public Exception getException(String msg, Throwable t) throws Exception { return new ValidationException(msg, t); }
/** * @see Validator#validate(Object) */ @Override public void validate(T item) throws ValidationException { if (!validator.supports(item.getClass())) { throw new ValidationException("Validation failed for " + item + ": " + item.getClass().getName() + " class is not supported by validator."); } BeanPropertyBindingResult errors = new BeanPropertyBindingResult(item, "item"); validator.validate(item, errors); if (errors.hasErrors()) { throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors), new BindException(errors)); } }
private String processFailedValidation(ValidatingItemProcessor<String> tested) { validator.validate(ITEM); when(validator).thenThrow(new ValidationException("invalid item")); return tested.process(ITEM); } }
@Override public void validate(Quad quad) throws ValidationException { boolean isValid = predicate.apply(quad); if(!isValid) { throw new ValidationException("A quad failed validation: " + quad); } }
@Override public void validate(Customer value) throws ValidationException { if(value.getFirstName().startsWith("A")) { throw new ValidationException("First names that begin with A are invalid: " + value); } } }
/** * @see Validator#validate(Object) */ @Override public void validate(T item) throws ValidationException { if (!validator.supports(item.getClass())) { throw new ValidationException("Validation failed for " + item + ": " + item.getClass().getName() + " class is not supported by validator."); } BeanPropertyBindingResult errors = new BeanPropertyBindingResult(item, "item"); validator.validate(item, errors); if (errors.hasErrors()) { throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors), new BindException(errors)); } }