CustomerDAO { public void insert(final Customer customer) throws SQLException, DuplicateException { if (LOG.isTraceEnabled()) { LOG.trace("CustomerDAO.insert(customer=" + customer + ")"); } if (isDuplicate(customer)) { throw new DuplicateException("Customers with duplicate names and/or IDs are not allowed. Customer: " + customer); } final Connection connection = getConnection(); final PreparedStatement statement = connection.prepareStatement("INSERT INTO customer VALUES ('" + customer.getId() + "', '" + customer.getName() + "');"); statement.executeUpdate(); connection.close(); } CustomerDAO(final String dataSourceJndi); int getNextId(); Customer get(final int id); List<Customer> getAll(); void insert(final Customer customer); void update(final Customer customer); void delete(final Customer customer); void clear();  }
@Test public void testInsert() throws SQLException, DuplicateException { final Customer customer = new Customer(1, "First customer name"); customerDAO.insert(customer); Assert.assertEquals(customer, customerDAO.get(1)); }
@Test(expected = DuplicateException.class) public void testInsertDuplicate() throws SQLException, DuplicateException { final Customer customer1 = new Customer(1, "First customer name"); final Customer customer2 = new Customer(2, "First customer name"); customerDAO.insert(customer1); customerDAO.insert(customer2); }