### Question:
DiscontinuationOrderRequestValidator implements Validator { @Override public void validate(Object obj, Errors errors) { final DiscontinuationOrderRequest discontinuationOrderRequest = (DiscontinuationOrderRequest) obj; if (discontinuationOrderRequest == null) { errors.reject("error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "orderer", "error.null"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "reasonNonCoded", "error.null"); } }  @Override boolean supports(Class clazz); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void shouldFailValidationIfDiscontinuationOrderRequestIsNull() throws Exception { Errors errors = new BindException(discontinuationOrderRequest, "discontinuationOrderRequest"); discontinuationOrderRequestValidator.validate(null, errors); assertTrue(errors.hasErrors()); assertThat(errors.getAllErrors() .size(), is(1)); assertThat((errors.getAllErrors()).get(0) .getCode(), is("error.general")); }
@Test public void shouldFailValidationIfOrdererIsNull() throws Exception { discontinuationOrderRequest.setOrderer(null); Errors errors = new BindException(discontinuationOrderRequest, "discontinuationOrderRequest"); discontinuationOrderRequestValidator.validate(discontinuationOrderRequest, errors); assertTrue(errors.hasErrors()); assertThat(errors.getAllErrors() .size(), is(1)); assertTrue(errors.hasFieldErrors("orderer")); assertFalse(errors.hasFieldErrors("reasonNonCoded")); }
@Test public void shouldFailValidationIfOrderReasonNonCodedIsNull() throws Exception { discontinuationOrderRequest.setReasonNonCoded(null); Errors errors = new BindException(discontinuationOrderRequest, "discontinuationOrderRequest"); discontinuationOrderRequestValidator.validate(discontinuationOrderRequest, errors); assertTrue(errors.hasErrors()); assertThat(errors.getAllErrors() .size(), is(1)); assertFalse(errors.hasFieldErrors("orderer")); assertTrue(errors.hasFieldErrors("reasonNonCoded")); discontinuationOrderRequest.setReasonNonCoded(""); errors = new BindException(discontinuationOrderRequest, "discontinuationOrderRequest"); discontinuationOrderRequestValidator.validate(discontinuationOrderRequest, errors); assertTrue(errors.hasErrors()); assertThat(errors.getAllErrors() .size(), is(1)); assertFalse(errors.hasFieldErrors("orderer")); assertTrue(errors.hasFieldErrors("reasonNonCoded")); discontinuationOrderRequest.setReasonNonCoded(" "); errors = new BindException(discontinuationOrderRequest, "discontinuationOrderRequest"); discontinuationOrderRequestValidator.validate(discontinuationOrderRequest, errors); assertTrue(errors.hasErrors()); assertThat(errors.getAllErrors() .size(), is(1)); assertFalse(errors.hasFieldErrors("orderer")); assertTrue(errors.hasFieldErrors("reasonNonCoded")); }
@Test public void shouldPassValidationIfAllFieldsAreCorrect() throws Exception { Errors errors = new BindException(discontinuationOrderRequest, "discontinuationOrderRequest"); discontinuationOrderRequestValidator.validate(discontinuationOrderRequest, errors); assertFalse(errors.hasErrors()); }