The previous answer is valid. However, if you can mock Iterable instead of ItemCollection, your life will be easier.
Iterable<Item> mockItemCollection = createMock(Iterable.class);
Iterator<Item> mockIterator = createMock(Iterator.class);
Item mockItem = new Item().with("attributeName", "Hello World");
expect(mockItemCollection.iterator()).andReturn(mockIterator);
expect(mockIterator.hasNext()).andReturn(true).andReturn(false);
expect(mockIterator.next()).andReturn(mockItem);
replay(mockItemCollection, mockIterator);
for(Item i : mockItemCollection) {
assertSame(i, mockItem);
}
verify(mockItemCollection, mockIterator);
BTW, I'm a big fan of static imports at least in the test code. It makes it more readable.
Reading the AWS code, I would consider their code to have a design flaw. It doesn't make sense to return a package scope class from a public interface. It's probably something that should be raised as an issue to them.
You could also always wrap the ItemCollection into a correctly typed class:
public class ItemCollectionWrapper<R> implements Iterable<Item> {
private ItemCollection<R> wrapped;
public ItemCollectionWrapper(ItemCollection<R> wrapped) {
this.wrapped = wrapped;
}
public Iterator<Item> iterator() {
return wrapped.iterator();
}
}