### Question:
HibernateHandler implements QueryHandler { @SuppressWarnings("unchecked") @Override public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) { try { org.hibernate.query.Query unwrappedQuery = query.unwrap(org.hibernate.query.Query.class); ScrollableResults results = unwrappedQuery.scroll(ScrollMode.FORWARD_ONLY); CloseableIterator<T> iterator = new ScrollableResultsIterator<T>(results); if (projection != null) { iterator = new TransformingIterator<T>(iterator, projection); } return iterator; } catch (PersistenceException e) { Iterator<T> iterator = query.getResultList().iterator(); if (projection != null) { return new TransformingIterator<T>(iterator, projection); } else { return new IteratorAdapter<T>(iterator); } } }  @Override void addEntity(Query query, String alias, Class<?> type); @Override void addScalar(Query query, String alias, Class<?> type); @Override boolean createNativeQueryTyped(); @SuppressWarnings("unchecked") @Override CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection); @SuppressWarnings("deprecation") @Override boolean transform(Query query, FactoryExpression<?> projection); @Override boolean wrapEntityProjections();  }### Answer:
@Test public void should_return_transforming_iterator_when_call_iterate_function() { ScrollableResultsImplementor scrollableResultsImplementor = createMock(ScrollableResultsImplementor.class); FactoryExpression<?> factoryExpression = createMock(FactoryExpression.class); expect(nativeQuery.unwrap(org.hibernate.query.Query.class)).andReturn(nativeQuery); expect(nativeQuery.scroll(FORWARD_ONLY)).andReturn(scrollableResultsImplementor); replay(nativeQuery); assertThat(hibernateHandler.iterate(nativeQuery, factoryExpression), instanceOf(TransformingIterator.class)); }
@Test public void should_return_iterator_adapter_when_call_iterate_function() { Query query = createMock(Query.class); List queryResultList = createMock(List.class); Iterator iterator = createMock(Iterator.class); expect(query.unwrap(org.hibernate.query.Query.class)).andThrow(new PersistenceException("Cannot unwrap Query")); expect(query.getResultList()).andReturn(queryResultList); expect(queryResultList.iterator()).andReturn(iterator); replay(query); assertEquals(IteratorAdapter.class, hibernateHandler.iterate(query, null).getClass()); }
@Test public void should_ReturnTransformingIterator_when_other_query_implementor() { Query query = createMock(Query.class); FactoryExpression<?> factoryExpression = createMock(FactoryExpression.class); List queryResultList = createMock(List.class); Iterator iterator = createMock(Iterator.class); expect(query.unwrap(org.hibernate.query.Query.class)).andThrow(new PersistenceException("Cannot unwrap Query")); expect(query.getResultList()).andReturn(queryResultList); expect(queryResultList.iterator()).andReturn(iterator); replay(query); assertEquals(TransformingIterator.class, hibernateHandler.iterate(query, factoryExpression).getClass()); }