From 2a420dd9470ecadb54278c47315097f9b18d63ca Mon Sep 17 00:00:00 2001 From: Fulminazzo Date: Tue, 9 Apr 2024 11:31:16 +0200 Subject: [PATCH] Added test for creation of item that returns null item meta --- .../angrybear/yagl/items/BukkitItemTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/item/bukkit/src/test/java/it/angrybear/yagl/items/BukkitItemTest.java b/item/bukkit/src/test/java/it/angrybear/yagl/items/BukkitItemTest.java index c0900f26..498eae8e 100644 --- a/item/bukkit/src/test/java/it/angrybear/yagl/items/BukkitItemTest.java +++ b/item/bukkit/src/test/java/it/angrybear/yagl/items/BukkitItemTest.java @@ -1,10 +1,13 @@ 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; @@ -12,11 +15,14 @@ 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 { @@ -94,6 +100,22 @@ void testCreateWithNullMetaFunction() { assertEquals(expected, actual); } + @Test + void testCreateWithNullMeta() { + ItemStack expected = new ItemStack(Material.STONE); + expected.setItemMeta(null); + try (MockedStatic bukkit = mockStatic(Bukkit.class); + MockedStatic 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")