ValidationUtils { public static void validateEmptyOrWhitespace(Errors errors, String errorMessageFormat, String... fields) { for (String field : fields) { rejectIfEmptyOrWhitespace(errors, field, String.format(errorMessageFormat, field)); } } private  ValidationUtils(); static void validateEmptyOrWhitespace(Errors errors, String errorMessageFormat, String... fields);  }
@Test public void shouldValidateIfDbUrlDbUsernameAndDbPasswordIsEmpty() throws Exception { when(errors.getFieldValue("field1")).thenReturn(""); when(errors.getFieldValue("field2")).thenReturn(" "); when(errors.getFieldValue("field3")).thenReturn(null); when(errors.getFieldValue("field4")).thenReturn("some value"); ValidationUtils.validateEmptyOrWhitespace(errors, "field_in_error_%s", "field1", "field2", "field3"); ArgumentCaptor<String> fieldCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(errors, times(3)).rejectValue(fieldCaptor.capture(), messageCaptor.capture(), aryEq((Object[]) null), anyString() ); List<String> rejectedFields = fieldCaptor.getAllValues(); assertThat(rejectedFields.size(), is(3)); assertThat(rejectedFields.get(0), is("field1")); assertThat(rejectedFields.get(1), is("field2")); assertThat(rejectedFields.get(2), is("field3")); List<String> errorMessages = messageCaptor.getAllValues(); assertThat(errorMessages.size(), is(3)); assertThat(errorMessages.get(0), is("field_in_error_field1")); assertThat(errorMessages.get(1), is("field_in_error_field2")); assertThat(errorMessages.get(2), is("field_in_error_field3")); }