### Question:
AbstractDatabaseTemplate implements DatabaseTemplate { @Override public <T extends Item> T fetchUnique(final Query query, final Class<T> itemClass) throws NonUniqueResultException { final Collection<T> items = this.fetch(query, itemClass); if (items.size() != 1) { throw new NonUniqueResultException(itemClass, items); } return items.iterator().next(); }  @Override T fetchUnique(final Query query, final Class<T> itemClass);  }### Answer:
@Test public void shouldFetchUnique_withSingleResultFromQuery() throws Exception { final StubItem item = new StubItem(); final Collection<StubItem> items = Arrays.asList(item); final Query mockQuery = mock(Query.class); final AbstractDatabaseTemplate databaseTemplate = new AbstractDatabaseTemplate() { @Override public <T extends Item> T read(final ItemId key, final Class<T> itemClass) throws NonExistentItemException { return null; } @Override public <T extends Item> T create(final T item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { return null; } @Override public <T extends Item> T update(final T item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { return null; } @SuppressWarnings("unchecked") @Override public <T extends Item> Collection<T> fetch(final Query query, final Class<T> itemClass) { return (Collection<T>) items; } @Override public GeneratedKeyHolder generateKeys(final SequenceKeyGenerator sequenceKeyGenerator) { return null; } @Override public void delete(final Item item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { } }; final StubItem returnedItem = databaseTemplate.fetchUnique(mockQuery, StubItem.class); assertEquals(item, returnedItem); }
@Test public void shouldNotFetchUnique_withMultipleResultFromQuery() throws Exception { final Collection<StubItem> items = Arrays.asList(new StubItem(), new StubItem(), new StubItem()); final Query mockQuery = mock(Query.class); final AbstractDatabaseTemplate databaseTemplate = new AbstractDatabaseTemplate() { @Override public <T extends Item> T update(final T item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { return null; } @Override public <T extends Item> T read(final ItemId key, final Class<T> itemClass) throws NonExistentItemException { return null; } @SuppressWarnings("unchecked") @Override public <T extends Item> Collection<T> fetch(final Query query, final Class<T> itemClass) { return (Collection<T>) items; } @Override public void delete(final Item item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { } @Override public <T extends Item> T create(final T item, final PersistenceExceptionHandler<?>... persistenceExceptionHandlers) { return null; } @Override public GeneratedKeyHolder generateKeys(final SequenceKeyGenerator sequenceKeyGenerator) { return null; } }; NonUniqueResultException actualException = null; try { databaseTemplate.fetchUnique(mockQuery, StubItem.class); } catch (final NonUniqueResultException e) { actualException = e; } assertNotNull(actualException); }