### Question:
CreateAction extends DatabaseAction<T> { @Override public void apply(final DatabaseTemplate databaseTemplate) throws Throwable { final T item = item(); item.setVersion(null); try { databaseTemplate.create(item()); } catch (final PersistenceException e) { handlePersistenceException(e); } } CreateAction(final T item, final List<PersistenceExceptionHandler<?>> persistenceExceptionHandlers); @Override void apply(final DatabaseTemplate databaseTemplate);  }### Answer:
@Test public void shouldApply_withDatabaseTemplateAndThrownException() throws Throwable { final StubItem item = mock(StubItem.class); final List<String> handlersCalled = new ArrayList<>(); final PersistenceExceptionHandler<OptimisticLockException> stubOptimisticLockExceptionHandler = new PersistenceExceptionHandler<OptimisticLockException>() { @Override public void handle(final OptimisticLockException exception) { handlersCalled.add(toString()); } @Override public Class<OptimisticLockException> getExceptionClass() { return OptimisticLockException.class; } @Override public String toString() { return "OptimisticLockExceptionHandler"; } }; final PersistenceExceptionHandler<ItemConstraintViolationException> stubItemConstraintViolationExceptionHandler = new PersistenceExceptionHandler<ItemConstraintViolationException>() { @Override public void handle(final ItemConstraintViolationException exception) { handlersCalled.add(toString()); } @Override public Class<ItemConstraintViolationException> getExceptionClass() { return ItemConstraintViolationException.class; } @Override public String toString() { return "ItemConstraintViolationExceptionHandler"; } }; final List<PersistenceExceptionHandler<?>> persistenceExceptionHandlers = new ArrayList<>(); persistenceExceptionHandlers.add(stubOptimisticLockExceptionHandler); persistenceExceptionHandlers.add(stubItemConstraintViolationExceptionHandler); final DatabaseTemplate mockDatabaseTemplate = mock(DatabaseTemplate.class); final OptimisticLockException optimisticLockException = new OptimisticLockException(null); when(mockDatabaseTemplate.create(item)).thenThrow(optimisticLockException); final CreateAction<StubItem> createAction = new CreateAction<StubItem>(item, persistenceExceptionHandlers); createAction.apply(mockDatabaseTemplate); assertThat(handlersCalled.size(), is(1)); assertThat(handlersCalled.iterator().next(), is("OptimisticLockExceptionHandler")); }
@Test public void shouldApply_withDatabaseTemplateAndThrownParentException() throws Throwable { final StubItem item = mock(StubItem.class); final List<PersistenceExceptionHandler<?>> persistenceExceptionHandlers = new ArrayList<>(); final List<String> handlersCalled = new ArrayList<>(); final PersistenceExceptionHandler<PersistenceException> stubPersistenceExceptionHandler = new PersistenceExceptionHandler<PersistenceException>() { @Override public void handle(final PersistenceException exception) { handlersCalled.add(toString()); } @Override public Class<PersistenceException> getExceptionClass() { return PersistenceException.class; } @Override public String toString() { return "PersistenceExceptionHandler"; } }; final PersistenceExceptionHandler<ItemConstraintViolationException> stubItemConstraintViolationExceptionHandler = new PersistenceExceptionHandler<ItemConstraintViolationException>() { @Override public void handle(final ItemConstraintViolationException exception) { handlersCalled.add(toString()); } @Override public Class<ItemConstraintViolationException> getExceptionClass() { return ItemConstraintViolationException.class; } @Override public String toString() { return "ItemConstraintViolationExceptionHandler"; } }; persistenceExceptionHandlers.add(stubPersistenceExceptionHandler); persistenceExceptionHandlers.add(stubItemConstraintViolationExceptionHandler); final DatabaseTemplate mockDatabaseTemplate = mock(DatabaseTemplate.class); final OptimisticLockException optimisticLockException = new OptimisticLockException(null); when(mockDatabaseTemplate.create(item)).thenThrow(optimisticLockException); final CreateAction<StubItem> createAction = new CreateAction<StubItem>(item, persistenceExceptionHandlers); createAction.apply(mockDatabaseTemplate); assertThat(handlersCalled.size(), is(1)); assertThat(handlersCalled.iterator().next(), is("PersistenceExceptionHandler")); }