### Question:
RequiredFieldValidator extends FieldValidatorSupport { public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); Object value = this.getFieldValue(fieldName, object); if (value == null) { addFieldError(fieldName, object); } else if (value.getClass().isArray() && Array.getLength(value) == 0) { addFieldError(fieldName, object); } else if (Collection.class.isAssignableFrom(value.getClass()) && ((Collection) value).size() == 0) { addFieldError(fieldName, object); } }  void validate(Object object);  }### Answer:
@Test public void testNullObject() throws Exception { RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class); rfv.setValueStack(ActionContext.getContext().getValueStack()); rfv.setFieldName("stringValue"); rfv.setDefaultMessage("${fieldName} field is required!"); ValidationAction action = new ValidationAction(); DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class)); rfv.setValidatorContext(context); rfv.validate(action); assertTrue(context.hasFieldErrors()); assertEquals(1, context.getFieldErrors().size()); assertNotNull(context.getFieldErrors().get("stringValue")); assertEquals("stringValue field is required!", context.getFieldErrors().get("stringValue").get(0)); }
@Test public void testArrayObject() throws Exception { RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class); rfv.setValueStack(ActionContext.getContext().getValueStack()); rfv.setFieldName("ints"); rfv.setDefaultMessage("${fieldName} field is required!"); ValidationAction action = new ValidationAction(); action.setInts(new Integer[]{}); DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class)); rfv.setValidatorContext(context); rfv.validate(action); assertTrue(context.hasFieldErrors()); assertEquals(1, context.getFieldErrors().size()); assertNotNull(context.getFieldErrors().get("ints")); assertEquals("ints field is required!", context.getFieldErrors().get("ints").get(0)); }
@Test public void testCollectionObject() throws Exception { RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class); rfv.setValueStack(ActionContext.getContext().getValueStack()); rfv.setFieldName("shorts"); rfv.setDefaultMessage("${fieldName} field is required!"); ValidationAction action = new ValidationAction(); action.setShorts(new ArrayList<Short>()); DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class)); rfv.setValidatorContext(context); rfv.validate(action); assertTrue(context.hasFieldErrors()); assertEquals(1, context.getFieldErrors().size()); assertNotNull(context.getFieldErrors().get("shorts")); assertEquals("shorts field is required!", context.getFieldErrors().get("shorts").get(0)); }