Skip to content

Commit

Permalink
Added test for creation of item that returns null item meta
Browse files Browse the repository at this point in the history
  • Loading branch information
fulminazzo committed Apr 9, 2024
1 parent e94b822 commit 2a420dd
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package it.angrybear.yagl.items;

import it.angrybear.yagl.ItemAdapter;
import it.angrybear.yagl.TestUtils;
import it.angrybear.yagl.items.fields.ItemFlag;
import it.fulminazzo.jbukkit.BukkitUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedStatic;

import java.lang.reflect.Method;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

class BukkitItemTest {

Expand Down Expand Up @@ -94,6 +100,22 @@ void testCreateWithNullMetaFunction() {
assertEquals(expected, actual);
}

@Test
void testCreateWithNullMeta() {
ItemStack expected = new ItemStack(Material.STONE);
expected.setItemMeta(null);
try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class);
MockedStatic<ItemAdapter> itemAdapter = mockStatic(ItemAdapter.class)) {
ItemFactory mockFactory = mock(ItemFactory.class);
when(mockFactory.getItemMeta(any())).thenReturn(null);
bukkit.when(Bukkit::getItemFactory).thenReturn(mockFactory);
itemAdapter.when(() -> ItemAdapter.itemToItemStack(any())).thenReturn(expected);

ItemStack actual = BukkitItem.newItem("stone").create(ItemMeta.class, m -> m.setDisplayName("Hello world"));
assertEquals(expected, actual);
}
}

private static Item mockItem(Item item) {
return item.setMaterial("STONE").setAmount(2).setDurability(15)
.setDisplayName("&7Cool stone").setLore("Click on this", "To be OP")
Expand Down

0 comments on commit 2a420dd

Please sign in to comment.