### Question:
RequiredChecker implements FieldConstraintDocChecker { @Override public void checkConstraint(ConstraintValidator validator, FieldTreeNode fieldMetadata, Path fieldMetadataPath, FieldConstraint constraint, JsonDoc doc) { if (((RequiredConstraint) constraint).getValue()) { List<Path> errors = getMissingFields(fieldMetadataPath, doc); for (Path x : errors) { validator.addDocError(Error.get(CrudConstants.ERR_REQUIRED, x.toString())); } } }  @Override void checkConstraint(ConstraintValidator validator,
                                FieldTreeNode fieldMetadata,
                                Path fieldMetadataPath,
                                FieldConstraint constraint,
                                JsonDoc doc); static List<Path> getMissingFields(Path fieldMetadataPath,
                                              JsonDoc doc);  }### Answer:
@Test public void testCheckConstraint_Deactivated() { ConstraintValidator validator = mock(ConstraintValidator.class); RequiredConstraint constraint = new RequiredConstraint(); constraint.setValue(false); new RequiredChecker().checkConstraint(validator, null, null, constraint, null); verify(validator, never()).addDocError(any(Error.class)); }
@Test public void testCheckConstraint_NoErrors() { ConstraintValidator validator = mock(ConstraintValidator.class); Path path = mock(Path.class); when(path.nAnys()).thenReturn(0); RequiredConstraint constraint = new RequiredConstraint(); constraint.setValue(true); JsonDoc doc = mock(JsonDoc.class); when(doc.get(path)).thenReturn(mock(JsonNode.class)); new RequiredChecker().checkConstraint(validator, null, path, constraint, doc); verify(validator, never()).addDocError(any(Error.class)); }
@Test public void testCheckConstraint_WithErrors() { ConstraintValidator validator = mock(ConstraintValidator.class); Path path = mock(Path.class); when(path.nAnys()).thenReturn(0); RequiredConstraint constraint = new RequiredConstraint(); constraint.setValue(true); JsonDoc doc = mock(JsonDoc.class); when(doc.get(path)).thenReturn(null); new RequiredChecker().checkConstraint(validator, null, path, constraint, doc); verify(validator, times(1)).addDocError(any(Error.class)); }
@Test public void testCheckConstraint_WithNullParent() { ConstraintValidator validator = mock(ConstraintValidator.class); Path path = mock(Path.class); when(path.nAnys()).thenReturn(0); RequiredConstraint constraint = new RequiredConstraint(); constraint.setValue(true); JsonDoc doc = mock(JsonDoc.class); when(doc.get(path)).thenReturn(JsonNodeFactory.instance.nullNode()); new RequiredChecker().checkConstraint(validator, null, path, constraint, doc); verify(validator, times(1)).addDocError(any(Error.class)); }