SpringValidationErrors implements ValidationErrors { @Override public void addError(Object bean, Field field, ValidatorAction va) { String fieldCode = field.getKey(); String errorCode = MessageUtils.getMessageKey(va, field); Object[] args = MessageUtils.getArgs(va, field); if (log.isDebugEnabled()) { log.debug("Rejecting value [field='" + fieldCode + "', errorCode='" + errorCode + "']"); } errors.rejectValue(fieldCode, errorCode, args, errorCode); }  void setErrors(Errors errors); Errors getErrors(); @Override void addError(Object bean, Field field, ValidatorAction va);  }
@Test public void testAddErrors01() throws Exception { Object bean = new Object(); Field field = new Field(); field.setKey("key"); FastHashMap hMsgs = new FastHashMap(); Msg msg = new Msg(); msg.setKey("messageKey"); hMsgs.put("name", msg); ReflectionTestUtils.setField(field, "hMsgs", hMsgs); @SuppressWarnings("rawtypes") Map[] args = new HashMap[4]; Arg arg = new Arg(); arg.setKey("arg0"); Map<String, Arg> hMap01 = new HashMap<String, Arg>(); hMap01.put("name", arg); args[0] = hMap01; arg = new Arg(); arg.setKey("arg1"); Map<String, Arg> hMap02 = new HashMap<String, Arg>(); hMap02.put("name", arg); args[1] = hMap02; arg = new Arg(); arg.setKey("arg2"); Map<String, Arg> hMap03 = new HashMap<String, Arg>(); hMap03.put("name", arg); args[2] = hMap03; arg = new Arg(); arg.setKey("arg3"); Map<String, Arg> hMap04 = new HashMap<String, Arg>(); hMap04.put("name", arg); args[3] = hMap04; ReflectionTestUtils.setField(field, "args", args); ValidatorAction va = new ValidatorAction(); va.setName("name"); SpringValidationErrors validation = new SpringValidationErrors(); ErrorsImpl01 errors = new ErrorsImpl01(); ReflectionTestUtils.setField(validation, "errors", errors); validation.addError(bean, field, va); ErrorsImpl01 assertErrors = (ErrorsImpl01) ReflectionTestUtils.getField( validation, "errors"); assertTrue(assertErrors.isRejectValue); assertEquals("key", assertErrors.field); assertEquals("messageKey", assertErrors.errorCode); Object[] objs = assertErrors.errorArgs; MessageSourceResolvable msr = null; for (int i = 0; i < objs.length; i++) { msr = (MessageSourceResolvable) objs[i]; String[] strs = msr.getCodes(); assertEquals("arg" + i, strs[0]); assertNull(msr.getArguments()); assertEquals("arg" + i, msr.getDefaultMessage()); } assertEquals("messageKey", assertErrors.defaultMessage); }