TreasureChest { ItemIterator iterator(ItemType itemType) { return new TreasureChestItemIterator(this, itemType); } TreasureChest(); List<Item> getItems();  }
@Test public void testIterator() { final TreasureChest chest = new TreasureChest(); final ItemIterator iterator = chest.iterator(expectedItem.getType()); assertNotNull(iterator); while (iterator.hasNext()) { final Item item = iterator.next(); assertNotNull(item); assertEquals(this.expectedItem.getType(), item.getType()); final String name = item.toString(); assertNotNull(name); if (this.expectedItem.toString().equals(name)) { return; } } fail("Expected to find item [" + this.expectedItem + "] using iterator, but we didn't."); }