### Question:
DiagnosisValidator implements Validator { @Override public void validate(Object o, Errors errors) { Diagnosis diagnosis = (Diagnosis)o; if(diagnosis == null){ throw new APIException("Diagnosis can't be null"); } else if (diagnosis.getVoided()) { return; } if (diagnosis.getEncounter() == null){ errors.rejectValue("encounter", "error.null"); } if (diagnosis.getDiagnosis() == null){ errors.rejectValue("diagnosis", "error.null"); } if (diagnosis.getCertainty() == null){ errors.rejectValue("certainty", "error.null"); } Integer rank = diagnosis.getRank(); if (rank == null){ errors.rejectValue("rank", "error.null"); }else if(rank < 0){ errors.rejectValue("rank", "error.rank.notPositiveInteger"); } }  @Override boolean supports(Class<?> aClass); @Override void validate(Object o, Errors errors);  }### Answer:
@Test public void validate_shouldFailValidationIfEncounterIsNull(){ diagnosis.setEncounter(null); new DiagnosisValidator().validate(diagnosis, errors); Assert.assertTrue(errors.hasFieldErrors("encounter")); }
@Test public void validate_shouldFailValidationIfDiagnosisIsNull(){ diagnosis.setDiagnosis(null); new DiagnosisValidator().validate(diagnosis, errors); Assert.assertTrue(errors.hasFieldErrors("diagnosis")); }
@Test public void validate_shouldFailValidationIfCertaintyIsNull(){ diagnosis.setCertainty(null); new DiagnosisValidator().validate(diagnosis, errors); Assert.assertTrue(errors.hasFieldErrors("certainty")); }
@Test public void validate_shouldFailValidationIfRankIsNull(){ diagnosis.setRank(null); new DiagnosisValidator().validate(diagnosis, errors); Assert.assertTrue(errors.hasFieldErrors("rank")); }
@Test public void validate_shouldFailValidationIfRankIsNonPositive(){ diagnosis.setRank(-1); new DiagnosisValidator().validate(diagnosis, errors); Assert.assertTrue(errors.hasFieldErrors("rank")); }