CustomerServiceImpl implements CustomerService { @Auditable(actionType = AuditActionType.DELETE) @Transactional public void deleteCustomer(Integer customerId) throws ParentChildConstraintException { Customer customer = customerDAO.findById(customerId); LOGGER.info("Deleting customer: " + customer); if (customer != null) { if (customer.getProjects() != null && !customer.getProjects().isEmpty()) { throw new ParentChildConstraintException(customer.getProjects().size() + " projects attached to client"); } else { try { customerDAO.delete(customer); } catch (DataIntegrityViolationException cve) { throw new ParentChildConstraintException(cve); } } } } CustomerServiceImpl(); @Transactional(readOnly = true) Customer getCustomer(String customerName, String customerCode); @Auditable(actionType = AuditActionType.DELETE) @Transactional void deleteCustomer(Integer customerId); @Transactional(readOnly = true) Customer getCustomer(Integer customerId); @Transactional(readOnly = true) Customer getCustomerAndCheckDeletability(Integer customerId); @Override @Transactional(readOnly = true) List<Customer> getCustomers(); @Override @Transactional(readOnly = true) List<Customer> getActiveCustomers(); @Transactional Customer persistCustomer(Customer customer); void setCustomerDAO(CustomerDao customerDAO);  }
@Test public void should_delete_customer() throws ParentChildConstraintException { int customerId = 2; Customer customer2 = new Customer(customerId); when(customerDAO.findById(customerId)).thenReturn(customer2); customerService.deleteCustomer(customerId); verify(customerDAO).delete(customer2); }
@Test public void should_delete_customer_with_projects_throw_a_constraint_violation() { Customer cust = new Customer(); Project proj = new Project(); Set<Project> projs = new HashSet<>(); proj.setProjectId(1); cust.setCustomerId(1); projs.add(proj); cust.setProjects(projs); when(customerDAO.findById(1)).thenReturn(cust); try { customerService.deleteCustomer(1); fail("no ParentChildConstraintException thrown"); } catch (ParentChildConstraintException e) { } }