### Question:
ValidatorStub implements Validator { @Override public void validate(Object target, Errors errors) { if (errorFields != null) { for (String field : errorFields) { errors.rejectValue(field, "Test Error"); } } if (hasGlobalErrors) { errors.reject("Test error"); } } ValidatorStub(String ... errorFields); void setGlobalError(); @Override boolean supports(Class<?> clazz); @Override void validate(Object target, Errors errors);  }### Answer:
@Test public void validateShouldRejectAllSpecifiedFields() { String[] fields = new String[]{"field1", "field2", "field3"}; ValidatorStub validator = new ValidatorStub(fields); TestBean testObject = new TestBean(); Errors errors = new BeanPropertyBindingResult(testObject, "object"); validator.validate(testObject, errors); assertEquals(3, errors.getErrorCount()); for (String field : fields) { assertTrue("Errors should contain field error for " + field, errors.hasFieldErrors(field)); } }
@Test public void validateShouldNotRejectFieldsIfNoErrorFieldSpecified() { ValidatorStub validator = new ValidatorStub(); TestBean testObject = new TestBean(); Errors errors = new BeanPropertyBindingResult(testObject, "object"); validator.validate(testObject, errors); assertFalse(errors.hasErrors()); }