@Test public void shouldRaiseIncorrectReferenceDataError() throws Exception { expectedEx.expect(dataExceptionMatcher("error.reference.data.missing")); doThrow(new DataIntegrityViolationException("foreign key")).when(mockedMapper).insert(product); repository.insert(product); }
public void insert(Product product) { try { mapper.insert(product); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.code"); } catch (DataIntegrityViolationException dataIntegrityViolationException) { String errorMessage = dataIntegrityViolationException.getMessage().toLowerCase(); if (errorMessage.contains("foreign key") || errorMessage.contains("violates not-null constraint")) { throw new DataException("error.reference.data.missing"); } else { throw new DataException("error.incorrect.length"); } } }
ProductRepository { public void insert(Product product) { try { mapper.insert(product); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.code"); } catch (DataIntegrityViolationException dataIntegrityViolationException) { String errorMessage = dataIntegrityViolationException.getMessage().toLowerCase(); if (errorMessage.contains("foreign key") || errorMessage.contains("violates not-null constraint")) { throw new DataException("error.reference.data.missing"); } else { throw new DataException("error.incorrect.length"); } } } }
ProductRepository { public void insert(Product product) { try { mapper.insert(product); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.code"); } catch (DataIntegrityViolationException dataIntegrityViolationException) { String errorMessage = dataIntegrityViolationException.getMessage().toLowerCase(); if (errorMessage.contains("foreign key") || errorMessage.contains("violates not-null constraint")) { throw new DataException("error.reference.data.missing"); } else { throw new DataException("error.incorrect.length"); } } }  }
ProductRepository { public void insert(Product product) { try { mapper.insert(product); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.code"); } catch (DataIntegrityViolationException dataIntegrityViolationException) { String errorMessage = dataIntegrityViolationException.getMessage().toLowerCase(); if (errorMessage.contains("foreign key") || errorMessage.contains("violates not-null constraint")) { throw new DataException("error.reference.data.missing"); } else { throw new DataException("error.incorrect.length"); } } }  void insert(Product product); void update(Product product); Long getIdByCode(String code); Product getByCode(String code); DosageUnit getDosageUnitByCode(String code); boolean isActive(String code); Integer getTotalSearchResultCount(String searchParam); List<DosageUnit> getAllDosageUnits(); Product getById(Long id); }
ProductRepository { public void insert(Product product) { try { mapper.insert(product); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.code"); } catch (DataIntegrityViolationException dataIntegrityViolationException) { String errorMessage = dataIntegrityViolationException.getMessage().toLowerCase(); if (errorMessage.contains("foreign key") || errorMessage.contains("violates not-null constraint")) { throw new DataException("error.reference.data.missing"); } else { throw new DataException("error.incorrect.length"); } } }  void insert(Product product); void update(Product product); Long getIdByCode(String code); Product getByCode(String code); DosageUnit getDosageUnitByCode(String code); boolean isActive(String code); Integer getTotalSearchResultCount(String searchParam); List<DosageUnit> getAllDosageUnits(); Product getById(Long id);  }