### Question:
FieldStateValidatorImpl implements FieldStateValidator { @Override public boolean validate(Collection<FormField> fields) { boolean isValid = true; for (FormField field : fields) { isValid = validate(field) && isValid; } return isValid; } @Inject  FieldStateValidatorImpl(TranslationService translationService); @PostConstruct void initialize(); @Override boolean validate(Collection<FormField> fields); @Override boolean validate(FormField field);  }### Answer:
@Test public void testValidateFieldUnexpectedWidget() { FormField field = mock(FormField.class); IsWidget widget = mock(IsWidget.class); when(field.getWidget()).thenReturn(widget); when(field.isRequired()).thenReturn(true); assertThatThrownBy(() -> fieldStateValidator.validate(field)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Unexpected widget type: impossible to read the value"); }
@Test public void testValidateCollectionInvalid() { boolean result = fieldStateValidator.validate(fieldCollection); assertFalse(result); }
@Test public void testValidateCollectionValid() { fieldCollection.forEach((FormField field) -> { when(field.isRequired()).thenReturn(false); }); boolean result = fieldStateValidator.validate(fieldCollection); assertTrue(result); }
@Test public void testValidateCustomValidatorsWithError() { when(textBox.getValue()).thenReturn("abcd"); CustomFieldValidator validator = o -> ValidationResult.error(MESSAGE); when(textBoxField.getCustomValidators()).thenReturn(Collections.singletonList(validator)); assertFalse(fieldStateValidator.validate(textBoxField)); verify(textBoxField).showError(eq(MESSAGE)); verify(textBoxField, never()).showWarning(any()); }
@Test public void testValidateCustomValidatorsWithWarning() { when(textBox.getValue()).thenReturn("abcd"); CustomFieldValidator validator = o -> ValidationResult.warning(MESSAGE); when(textBoxField.getCustomValidators()).thenReturn(Collections.singletonList(validator)); assertTrue(fieldStateValidator.validate(textBoxField)); verify(textBoxField).showWarning(eq(MESSAGE)); verify(textBoxField, never()).showError(any()); }
@Test public void testValidateCustomValidatorsWithSucces() { when(textBox.getValue()).thenReturn("abcd"); CustomFieldValidator validator = o -> ValidationResult.valid(); when(textBoxField.getCustomValidators()).thenReturn(Collections.singletonList(validator)); assertTrue(fieldStateValidator.validate(textBoxField)); verify(textBoxField, never()).showWarning(any()); verify(textBoxField, never()).showError(any()); }