@Test public void shouldThrowExceptionIfDataLengthIsIncorrectWhileInsertingProductCategory() { ProductCategory productCategory = new ProductCategory(); doThrow(new DataIntegrityViolationException("some error")).when(mapper).insert(productCategory); expectedException.expect(dataExceptionMatcher("error.incorrect.length")); repository.insert(productCategory); }
public void insert(ProductCategory productCategory) { try { mapper.insert(productCategory); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("product.category.name.duplicate"); } 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"); } } }
ProductCategoryRepository { public void insert(ProductCategory productCategory) { try { mapper.insert(productCategory); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("product.category.name.duplicate"); } 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"); } } } }
ProductCategoryRepository { public void insert(ProductCategory productCategory) { try { mapper.insert(productCategory); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("product.category.name.duplicate"); } 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"); } } } @Autowired  ProductCategoryRepository(ProductCategoryMapper mapper); }
ProductCategoryRepository { public void insert(ProductCategory productCategory) { try { mapper.insert(productCategory); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("product.category.name.duplicate"); } 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"); } } } @Autowired  ProductCategoryRepository(ProductCategoryMapper mapper); void insert(ProductCategory productCategory); void update(ProductCategory productCategory); ProductCategory getExisting(ProductCategory productCategory); Long getIdByCode(String categoryCode); ProductCategory getByCode(String code); List<ProductCategory> getAll(); ProductCategory getById(Long id); }
ProductCategoryRepository { public void insert(ProductCategory productCategory) { try { mapper.insert(productCategory); } catch (DuplicateKeyException duplicateKeyException) { throw new DataException("product.category.name.duplicate"); } 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"); } } } @Autowired  ProductCategoryRepository(ProductCategoryMapper mapper); void insert(ProductCategory productCategory); void update(ProductCategory productCategory); ProductCategory getExisting(ProductCategory productCategory); Long getIdByCode(String categoryCode); ProductCategory getByCode(String code); List<ProductCategory> getAll(); ProductCategory getById(Long id);  }