### Question:
InventoryUtil { public static int count(@NotNull Inventory inv, @NotNull ItemStack item) { int amount = 0; for (ItemStack invItem : inv.getContents()) { if (item.isSimilar(invItem)) { amount += invItem.getAmount(); } } return amount; }  @SuppressWarnings("deprecation") static void removeInvItem(@NotNull final Inventory inv, @NotNull final Material type, final int amount,
                                     final MaterialData data); static void removeInvItem(final Inventory inv, final ItemStack item, final boolean matOnly); static void removeInvItem(@NotNull final Inventory inv, @NotNull final ItemStack item); static void removeInvMat(@NotNull final Inventory inv, @NotNull final Material material, final int amount); static boolean contains(@Nullable final Inventory inv, @Nullable final ItemStack item, int amount); static int getTotalItems(final Inventory inv, final Material type); static int countItems(@NotNull final Player player, final Material material); static int countItems(@NotNull final Player player, final Material material, final short damageValue); static int count(@NotNull Inventory inv, @NotNull ItemStack item); @SuppressWarnings("deprecation") static boolean canHold(@NotNull final Player player, final ItemStack item); static boolean canHold(final Player player, @NotNull final Material material, final byte data,
                                  final int quantity); static int emptySlots(final Inventory inv); @NotNull static ItemStack getPlayerSkull(@NotNull final OfflinePlayer player); static ItemStack[] getPlayerArmor(@Nullable Inventory inv);  }### Answer:
@Test public void testCountOneEqualElement() { int amount = 16; ItemStack item = new ItemStack(Material.APPLE, amount); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {item}); Assert.assertEquals(amount, InventoryUtil.count(inv, item)); }
@Test public void testCountNoItems() { Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {}); Assert.assertEquals(0, InventoryUtil.count(inv, new ItemStack(Material.APPLE))); }
@Test public void testCountDuplicate() { int amount = 16; ItemStack item = new ItemStack(Material.APPLE, amount); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {item, item}); Assert.assertEquals(2 * amount, InventoryUtil.count(inv, item)); }
@Test public void testCountDuplicateNewInstance() { int amount = 16; ItemStack itemA = new ItemStack(Material.APPLE, amount); ItemStack itemB = new ItemStack(Material.APPLE, amount * 2); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {itemA, itemB}); assertTrue(itemA.isSimilar(itemB)); Assert.assertEquals(amount * 3, InventoryUtil.count(inv, itemA)); }
@Test public void testCountOtherItems() { int amount = 16; ItemStack itemA = new ItemStack(Material.APPLE, amount); ItemStack itemB = new ItemStack(Material.GOLDEN_APPLE, amount / 2); Inventory inv = mock(Inventory.class); when(inv.getContents()).thenReturn(new ItemStack[] {itemA, itemB}); Assert.assertEquals(amount, InventoryUtil.count(inv, itemA)); }