@Test public void shouldThrowDuplicateKeyExceptionWhenDuplicateProductGroupCodeFound() throws Exception { expectedEx.expect(dataExceptionMatcher("error.duplicate.product.group.code")); ProductGroup productGroup = new ProductGroup(); doThrow(new DuplicateKeyException("")).when(mapper).insert(productGroup); repository.insert(productGroup); }
public void insert(ProductGroup productGroup) { try { mapper.insert(productGroup); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.group.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"); } } }
ProductGroupRepository { public void insert(ProductGroup productGroup) { try { mapper.insert(productGroup); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.group.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"); } } } }
ProductGroupRepository { public void insert(ProductGroup productGroup) { try { mapper.insert(productGroup); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.group.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"); } } }  }
ProductGroupRepository { public void insert(ProductGroup productGroup) { try { mapper.insert(productGroup); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.group.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(ProductGroup productGroup); ProductGroup getByCode(String code); void update(ProductGroup productGroup); List<ProductGroup> getAll(); }
ProductGroupRepository { public void insert(ProductGroup productGroup) { try { mapper.insert(productGroup); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("error.duplicate.product.group.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(ProductGroup productGroup); ProductGroup getByCode(String code); void update(ProductGroup productGroup); List<ProductGroup> getAll();  }