### Question:
ConditionValidator implements Validator { @Override public void validate(Object object, Errors errors) { if(object == null){ throw new IllegalArgumentException("The object parameter should not be null"); } if(!(object instanceof Condition)){ throw new IllegalArgumentException("The object parameter should be of type " + Condition.class); } Condition condition = (Condition) object; if(condition.getCondition() == null){ errors.rejectValue("condition", "Condition.conditionShouldNotBeNull", "The condition is required"); } if(condition.getClinicalStatus() == null){ errors.rejectValue("clinicalStatus", "Condition.clinicalStatusShouldNotBeNull", "The clinical status is required"); } }  @Override boolean supports(Class<?> aClass); @Override void validate(Object object, Errors errors);  }### Answer:
@Test public void shouldFailIfGivenNull(){ expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(NULL_ERROR_MESSAGE); validator.validate(null, errors); }
@Test public void shouldFailIfGivenInstanceOfClassOtherThanCondition(){ expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(INCOMPATIBLE_ERROR_MESSAGE); validator.validate(new Cohort(), errors); }
@Test public void shouldFailIfGivenConditionWithNullConditionProperties(){ Condition condition = new Condition(); validator.validate(condition, errors); Assert.assertTrue(errors.hasFieldErrors("condition")); Assert.assertTrue(errors.hasFieldErrors("clinicalStatus")); }
@Test public void shouldPassIfConditionClassIsPassedWithRequiredConditionProperties(){ Condition condition = new Condition(); condition.setCondition(new CodedOrFreeText(new Concept(), new ConceptName("name", new Locale("en")), "nonCoded")); condition.setClinicalStatus(ConditionClinicalStatus.ACTIVE); validator.validate(condition, errors); Assert.assertFalse(errors.hasFieldErrors("condition")); Assert.assertFalse(errors.hasFieldErrors("clinicalStatus")); }